I am practicing to create a small project with different files to have a clean code. I want to show yellow frame from (fyellow.py) into (main.py) and input a label into it from (funbut.py) using Button's function. This is my code example: (3 Python files - main.py, fyellow.py, and funbut.py)
main.py
from tkinter import *
from fyellow import *
import funbut
root = Tk()
root.geometry("500x500")
# Show Yellow Frame into Main from (fyellow.py)
myframe = Frameyellow(root)
# Button with command - But_fun1
but1 = Button(root, text="Text",command=funbut.but_fun1)
but1.pack()
root.mainloop()
funbut.py
from tkinter import *
from fyellow import *
# Function of Button (but1) PROBLEM HERE! (ERROR - 'framey' is not defined)
def but_fun1():
label1 = Label(framey,text="LabelText")
label1.place(x=10,y=10)
fyellow.py
from tkinter import *
class Frameyellow:
def __init__(self,rootyellow):
self.rootyellow = rootyellow
self.framey = Frame(rootyellow, width=200,height=200,bg="yellow")
self.framey.pack()
Could explain what can I do to use the self.framey from file (fyellow.py) to avoid
error 'framey' is not defined?
So main.py file would look like this:
from tkinter import Tk, Button
from fyellow import FrameYellow
from funbut import place_label
root = Tk()
root.geometry("500x500")
my_frame = FrameYellow(root)
my_frame.pack()
but1 = Button(root, text="Text", command=lambda: place_label(my_frame))
but1.pack()
root.mainloop()
fyellow.py like this (tho kinda pointless to create a class whose sole purpose is to have the frame a different color, just use arguments and create a normal frame):
from tkinter import Frame
class FrameYellow(Frame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs, bg='yellow')
and funbut.py should be sth like this:
from tkinter import Label
def place_label(parent, text='Text', pos=(0, 0)):
Label(parent, text=text).place(x=pos[0], y=pos[1])
Also:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.
Related
I am trying a "Hello World" test on a GUI window, imported from tkinter. The problem starts when I try to run it and an error pops up in the terminal of VScode:
cannot import name 'geometry' from 'tkinter'.
import csv
from fileinput import filename
import os
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
from tkinter import geometry
class attendance_tester(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
root = Tk()
root.geometry("800x800")
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
app = attendance_tester(master=root)
app.master.title("Student Attendance Program")
app.mainloop()
root.destroy()
from attendance_create import (func_create, func_edit_add,
func_ask_edit, func_edit_change, func_edit_sub,
func_edit_view, func_open, func_first_change)
with open(r"C:\\Users\\user_name\\Desktop\\attendance_sheet.csv") as file:
csv_file_read = csv.reader(file)
for row in csv_file_read:
print(row)
func_open()
attend_test = attendance_tester(root)
root.mainloop()
I have tried seemingly every solution I could find on the internet. I really need some help.
You don't need to import geometry to use it. Just remove the import line and it should work.
you shouldnt import it from the first place you just need to import the tkinter itself just remove that line and it should be just fine
There is no reason to import geometry, it works without that import line.
Also, you have a few duplicate imports which may become confusing. It's better to stick to fewer imports:
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
In the following code I get an error when leaving out "from tkinter import *. I am confused as to the reason as I import tkinter as tk.
I tried modifying the function set_text without success.
The error is on
self.e.delete(0,END)
NameError: name 'END' is not defined`
Code:
from tkinter import *
import tkinter as tk
from tkinter import ttk
class HelperFun():
def set_text(self,parent,text):
self.e.delete(0,END)
self.e.insert(0,text)
return
def __init__(self,parent):
self.parent=parent
self.e = tk.Entry(self.parent,width=10)
self.e.pack()
self.b1 = tk.Button(self.parent,text="animal",
command=lambda:self.set_text(self.parent,"animal"))
...
root=tk.Tk()
HelperFun(root)
root.mainloop()
Thank you.
When you use, from Tkinter import *, it imports all of the constants from that package, like END.
To get away without using this, you'd need to use the dot operator, like tk.END, otherwise it won't be defined.
If you import tkinter as tk then you need to refer to END as tk.END.
Importing from tkinter import * imports everything in tkinter, making it so you don't have to use the tk qualifying prefix.
replace the line
self.e.delete(0,END)
with
self.e.delete(0,"end")
Your code isn't doing anything. And it is not showing Button
You cannot use self.e simultaneously in the set text() function.
The Button command isn't right.
I modified the code to make it more readability:
import tkinter as tk
class HelperFun():
def __init__(self, parent):
self.parent=parent
self.e = tk.Entry(self.parent,width=10)
self.e.pack()
self.b1 = tk.Button(self.parent,text="animal",
command=lambda:self.set_text(self.e.get()))
self.b1.pack()
self.lb = tk.Label(self.parent, width=10)
self.lb.pack()
def set_text(self, _text):
print(_text)
self.lb.configure(text=_text)
return
root=tk.Tk()
app = HelperFun(root)
root.mainloop()
Screenshot before and after clicking button:
I think in "(0,END)" you need to use 1.0 instead of 0.
Try "(1.0,END)"
END is like Button and Label ...
u have to write tk.END if u (import tkinter as tk)
so I am trying to create a button on tkinter that runs another script. I'm not sure whether this is the most optimal way rather than just adding whatever I have onto the script I want to run but I want to see if this is possible for future references. This is what I have on my script so far. Any ideas on how to approach this?
import tkinter as tk
from tkinter import *
from tkinter import simpledialog
import uploadtest as ut
class Initial(simpledialog.Dialog):
def body(self, master):
#input fields for username and passwords
Label(master, text="Scripts").grid(row=1, column=1),
#Buttons
self.utz = ut
self.b1 = Button(master, text = "Script1", bg="grey", command=self.utz)
self.b1.grid(row=7, column=1, ipadx=75)
root = tk.Tk()
root.withdraw()
d = Initial(root)
Any help would be greatly appreciated. I know I can just add this class to the other script and use the button command and it would be easier, but I was just curious if this method was possible since I am going to be adding multiple scripts to this and I would like to have my scripts separated. It seems like when I try to import uploadtest.py on this script, it just runs that script instead of this one.
if you want to use it again for future references you can modify your uploadtest.py to be a function
def ut():
print("your")
print("script")
print("here")
then to use it in the script do from uploadtest import * and call it like a normal function that's in your script
import tkinter as tk
from tkinter import *
from tkinter import simpledialog
from uploadtest import *
class Initial(simpledialog.Dialog):
def body(self, master):
#input fields for username and passwords
Label(master, text="Scripts").grid(row=1, column=1),
#Buttons
self.utz = ut
self.b1 = Button(master, text = "Script1", bg="grey", command=self.utz)
self.b1.grid(row=7, column=1, ipadx=75)
root = tk.Tk()
root.withdraw()
d = Initial(root)
please comment if this is not what your looking for
I have little python programs to create html, for example insertMedia.py: this takes the name of the media file, puts it in the html code string and writes it to the output file.
insertMedia.py uses tkinter. I have a window with the buttons "insert audio" and 'insert video".
insertMedia.py works well, no problems. Also, I have some more "makehtml" programs, each for a specific task.
I want to have a master window, with buttons, or maybe a menu item for each "makehtml" program.
How can I open and run insertMedia.py from a master window?
Should I put all the "makehtml" programs in a module and import the module?
This code from Bryan Oakley, here in stackoverflow, opens a new window. Can it be modified to open my insertMedia.py?
import Tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()
To show you just the priciple of a common way to do this.
Frist you create a main file with your stuff in main.py wich looks like for exampel:
main.py
import tkinter as tk
import Mod1
root = tk.Tk()
def callback():
m1 = Mod1.Model(root, var='my_html')
b = tk.Button(text='click me', command=callback)
b.grid(column=0,row=1)
root.mainloop()
Then you are creating another script with your Toplevel and stuff in it like this:
Mod1.py
from __main__ import tk
class Model(tk.Toplevel):
def __init__(self, master, var=None):
tk.Toplevel.__init__(self, master)
self.master = master
self.configure(bg="red", width=300, height=300)
b=tk.Button(self,text='print html', command=lambda:print(var))
b.pack()
So what I did here is to create another script with a class with the parent class tk.Toplevel. This means the class becomes a subclass of it. We pass the parameter master wich is root, as you can see in the main.py to get a reference through the interface.
Also note that we imported the Mod1 script and later we reference the Model by Mod1.Model(). You could just import the Model with from Mod1 import Model instead of import Mod1. Also we imported from the main script the reference of tk to work with.
To pass a variabel like a string of your html, you can pass var through the interface. I made it like a kwarg(keyword argument), to make it necessary for the class to initialize you have to delete =None wich is a default vaulue.
Thats all we need to work it out. Have fun!
For more complex stuff you also could do a dictionary with keys and values.
main.py
import tkinter as tk
import Mod1
root = tk.Tk()
def callback():
data_dict = {'key a':'value a','key b':'value b'}
m1 = Mod1.Model(root, dct= data_dict)
b = tk.Button(text='click me', command=callback)
b.grid(column=0,row=1)
root.mainloop()
and Mod1.py
from __main__ import tk
class Model(tk.Toplevel):
def __init__(self, master, dct=None):
tk.Toplevel.__init__(self, master)
self.master = master
self.configure(bg="red", width=300, height=300)
b=tk.Button(self,text='print html', command=lambda:print(dct['key a']))
b.pack()
I'm trying to make a simple outline for a gui, and I'm getting the warning
"variable" May be undefined or defined from star imports: tkinter for all of my variables.
Here is my code:
from tkinter import *
class myApp :
def __init__(self, gui,) :
self.root = gui
self.bframe = Frame(self.root) # Create a container Frame at bottom
self.bframe.pack(side=BOTTOM)
self.xlabel = Label(self.root, text="Item ID") # Create the Label
self.xlabel.pack(side=LEFT)
self.xentry = Entry(self.root, bd=5) # Create the Entry box
self.xentry.pack(side=LEFT)
self.xentry.bind('<Return>', self.showStockItem)
self.xentry.focus_set() # Set focus in the Entry box
self.xopen = Button(self.root, text="Show", command=self.showStockItem) # Create the open Button
self.xopen.pack(side=LEFT)
self.xquit = Button(self.bframe, text="Quit", command=self.quitit) # Create the quit Button
self.xquit.pack(side=BOTTOM)
return
gui = Tk()
gui.title("Travel")
app = myApp(gui)
gui.mainloop()
from tkinter import *
In this line, you import everything from tkinter. This is not recommended, so linter will warn you. But if you really want to do this, it's OK, just ignore it.
To be better, you should explicitly import what you need. For example:
from tkinter import Tk, Label, Frame, Entry, Button
Consider using:
import tkinter as tk
and then, prefix all your calls like:
root = tk.Tk()
or,
variableName.pack(side = tk.LEFT)
and so on...