When I created this module I first made the tkinter window (all of its settings globally) it worked as intended. I could run the module and the window worked, taking the input from the entry field and displaying the welcome or error message. But when I put them into a function, it stopped working correctly, as shown.
How the window looks when created globally, with the button and input working:
https://gyazo.com/ffcb16416b8a971c09bfa60ee9367bbd
How it looks when created inside the function:
https://gyazo.com/c8858a2793befafa41e71d1099f021d3
The error message pops up straight away, then the main window with the entry field but not the button.
Here's the code where I created the window and settings inside a function:
def userSign(userEntry):
userId = userEntry.get()
if userId.isdigit() == True and len(userId) == 4:
welcomeWindow = tkinter.Tk()
welcomeWindow.title("Welcome")
welcomeWindow.geometry("200x50")
welcome = tkinter.Label(master=welcomeWindow, text="Welcome "+userId,font=("Helvetica", 18, "bold"))
welcome.grid()
welcomeWindow.mainloop()
else:
errorWindow = tkinter.Tk()
errorWindow.title("ERROR")
errorWindow.geometry("500x50")
error = tkinter.Label(master=errorWindow, text="ERROR: "+userId +" DOES NOT MEET CRITERIA", font=("Helvetica", 18, "bold"))
error.grid()
userId=""
errorWindow.mainloop()
def show():
window = tkinter.Tk()
window.title("Sign In")
window.geometry("250x100")
signInPrompt = tkinter.Label(master = window, text = "Enter your ID to sign in")
signInPrompt.grid(column=0,row=2)
userEntry = tkinter.Entry(master = window)
userEntry.grid(column=0,row=4)
enterButton = tkinter.Button(master = window, text="Sign in", command=userSign(userEntry))
enterButton.grid(column=0,row=6)
window.mainloop()
How do I get it so that my window works correctly when created inside functions as this module needs to be called by a different, main module.
You are creating two instances of Tk() which is a bad idea. Instead use Toplevel() for additional windows.
When you create variables or widgets inside a function the names are in the local scope and not available outside the function. And whan the function ends they will be garbage colletced.
Also, as #fhdrsdg points out, problems in the button command.
Related
The code below shows part of my program and the issue im facing.
def checkAnswer():
mainAnswer = answer01.get()
if len(mainAnswer) == 0:
messagebox.showwarning(message='Please answer the question!')
return
if int(mainAnswer) != answer:
messagebox.showwarning(message='Incorrect! The correct answer is: ' + str(answer))
else:
nxtquest.config(state=NORMAL)
messagebox.showinfo(message='Correct! :)')question01 = Label(easy)
question01.grid(row=2, column=0)
answer01 = Entry(easy)
answer01.grid(row=3, column=2)
answer01.bind('<Return>', func=lambda e:checkAnswer())
start = Button(easy, text = "Start!", command=ask, bg='green', fg='white')
start.grid(row=3, column=3)
nxtquest = Button(easy, text='Next Question', command=ask)
nxtquest.grid(row=5, column=2)
checkbut = Button(easy, text='Check', command=checkAnswer)
checkbut.grid(row=4, column=2)
#check button and answer01 enabled after start pressed
launch = 1
if launch == 1:
answer01.config(state=DISABLED)
checkbut.config(state=DISABLED)
nxtquest.config(state=DISABLED)
The issue which im struggling here is that whenever i run the program everything is okay. When the window is displayed checkbut, nxtquest and label answer01 are greyed out (disabled).
The start button enables only checkbut and answer01 and then is destroyed. (So far so good)
So nxtquest will enable once the input is correct as seen in the
else:
nxtquest.config(state=NORMAL)
But when I reach another question the nxtquest button is already enabled, this is the problem!
How could I make it so the button will enable itself only after the warning message box is displayed?
Could I ask for some help with this and possibly suggestions if you see any rookie mistakes ?
Whilst I don't know of any way you could do this with a messagebox widget (although I'm sure there's an event you could use as the trigger) you can most certainly do this by substituting the messagebox with a Toplevel widget and using .protocol("WM_DELETE_WINDOW", callback()) on the widget.
This would mean that whenever the Toplevel widget was "closed" we would actually be overwriting the action taken when the event was raised and would manually handle the closing of the widget as well as whatever else we wanted it to do.
This would look something like the below:
from tkinter import *
root = Tk()
button = Button(root, text="Ok", state="disabled")
button.pack()
top = Toplevel(root)
def close():
top.destroy()
button.configure(state="active")
top.protocol("WM_DELETE_WINDOW", close)
root.mainloop()
If you close the Toplevel widget you will see that the button is now active instead. This would equally work if we added a Button to the Toplevel widget which called the function close().
I've got an interesting problem with the tk Entry widget. If I run the following test code,
from Tkinter import *
root =Tk()
def pfunc(self):
print Input.get()
f=Frame(root)
f.pack()
Input=Entry(f)
#Input.bind("<Return>",pfunc)
Input.pack()
root.mainloop()
I can properly enter into the widget and print to console; however the following code, as part of a larger GUI, does not allow me to click in the Entry boxes at all.
self.Tlabel = Label(self.TempFrame, text="Temp")
self.Tlabel.pack( side = LEFT)
self.Tenter = Entry(self.TempFrame,width=10, bd =5)
self.Tenter.bind("<Return>",self.getFlux)
self.Tenter.pack (side=RIGHT)
self.Flabel = Label(self.FluxFrame, text="Flux")
self.Flabel.pack( side = LEFT)
self.Fenter = Entry(self.FluxFrame, width=10, bd =5)
self.Fenter.bind("<Return>",self.getTemp)
self.Fenter.pack(side = RIGHT)
def getFlux(self):
for i in range(len(self.fit_tuples)):
if self.fit_tuples[i][0]==self.currentBFMdate and self.fit_tuples[i][1]==self.cell.get():
fit_data=self.fit_tuples[i][2]
self.Fenter.set(fit_data[0]*np.exp(fit_data[1]*int(self.Tenter.get())))
else:
self.Fenter.set("Invalid")
def getTemp(self):
for i in range(len(self.fit_tuples)):
if self.fit_tuples[i][0]==self.currentBFMdate and self.fit_tuples[i][1]==self.cell.get():
fit_data=self.fit_tuples[i][2]
self.Tenter.set(np.log(float(self.Fenter.get())/fit_data[0])/fit_data[1])
else:
self.Tenter.set("Invalid")
Furthermore, if I run both codes on a separate windows PC I have the same problem. The only difference I can possibly think of is that I am using instance variables within a class; but it seems that other widgets are bound and working properly.
Basically, the bind method is passing "Return" as a parameter to getTemp. As another user suggested, just add another parameter to the function.
If you use bind method, callback is called with an event object. You should add event parameter to the method/function. (See Events and Bindings - An Introduction to Tkinter )
So, rpelcae following lines:
def getFlux(self, event):
...
def getTemp(self, event):
...
The first program work unintentionally. Its parameter name should be event, not self.
I don't understand why the entry boxes under rackGUI.py in my code are static/won't allow anything to be entered. I believe all the Entry objects are instantiated correctly. I specified the textvariable as instances of the StringVar(). My gut tells me the problem lies in command argument in create_button instantiation but I'm not really sure why. I thought by setting command = lambda:function the function would not be called.
Upon clicking 'New' in the menu, main.py successfully calls rackGUI.create() which successfully calls input_form(). Clicking the button 'create_button' successfully calls drawRack which prints to the shell 'test'. I also added a test where I printed the type of value for each entry box i.e., print type(rack_name.get()) and this successfully returns type 'str'.
So again the main problem is that the entry box is static.
Below is my code:
config.py
"""
config.py
"""
import Tkinter as tk
import tkMessageBox as tkmb
#setup
root = tk.Tk()
root.title("TLA Database Tool")
frame = tk.Frame(height = 300, width = 250)
frame.pack()
main.py
#main.py
from config import *
import rackGUI
def createRackTemplate():
rackGUI.create()
def loadRackTemplate():
rackGUI.load()
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar)
filemenu.add_command(label = "New", command = createRackTemplate)
filemenu.add_command(label = "Load", command = loadRackTemplate)
menubar.add_cascade(label = "File", menu = filemenu)
tkmb.showinfo("Welcome", "Under File click New to create a new rack template.\n\
Click load to load rack template.")
root.config(menu = menubar)
root.mainloop()
rackGUI.py
"""
rackGUI.py
"""
from config import *
def input_form():
form_frame = tk.Frame(frame)
form_frame.pack()
tk.Label(form_frame, text = "Rack Template Name (e.g., Knox Type 4)").pack()
rack_name = tk.Entry(form_frame, textvariable = tk.StringVar())
rack_name.pack()
tk.Label(form_frame, text = "Dimensions").pack()
tk.Label(form_frame, text = "#rack rows").pack()
num_rack_rows = tk.Entry(form_frame, textvariable = tk.StringVar())
num_rack_rows.pack()
tk.Label(form_frame, text = "#nodes per row").pack()
num_slots = tk.Entry(form_frame, textvariable = tk.StringVar())
num_slots.pack()
create_button = tk.Button(form_frame, text = "Create!",\
command = lambda: drawRack(rack_name, num_rack_rows, num_slots))
create_button.pack()
def drawRack(rack_name, num_rack_rows, num_slots):
print rack_name.get(), num_rack_rows.get(), num_slots.get()
def create():
input_form()
def load():
pass
For anyone who comes here after me, my solution ended up being
root.overrideredirect(True)
Working fine on Windows, but causing this text entry problem on Mac.
I actually found the problem there. The issue seems to be the focus of the windows, since you're using a messagebox.
In my script I just had put a root.update() before opening another window (in my case a filedialog) and everything worked fine. There's an already existing issue for that: https://bugs.python.org/issue42867#msg384785
I use the tkinter function to create a new window, it works fine.
When I link from this window to another window, the button moves to the first window. I don't understand why it moves.
Here is the code for the first window,
import tkinter
window = tkinter.Tk()
window.title ("Login")
window.geometry ("300x150")
username = "Gurdip"
password = "1234"
def login():
if txtUser.get() == username and txtPass.get() == password:
import NewWindow
lblUser = tkinter.Label(window, text="Username:")
lblUser.pack()
txtUser = tkinter.Entry(window)
txtUser.pack()
lblPass = tkinter.Label(window, text="Password:")
lblPass.pack()
txtPass = tkinter.Entry(window)
txtPass.pack()
btnenter = tkinter.Button(window, text="Enter", command=login)
btnenter.pack()
And for the second window
import tkinter
window = tkinter.Tk()
window.title ("The Royal Liberty School")
window.geometry ("300x150")
def webpage():
import webbrowser
webbrowser.open("http://www.royalliberty.org.uk/")
lblRlib = tkinter.Label(window, text="Welcome to the Royal Liberty School\n\nClick the link to go to our website")
lblRlib.pack()
def button():
webbutton = tkinter.Button(text ="Royal Liberty School", command = webpage)
webbutton.pack()
button()
My guess is that you're reporting that the "Royal Liberty School" button is appearing on the wrong window, rather than actually moving. I've never heard of a button moving before.
if that guess is correct, it's probably because you aren't giving it an explicit parent, so it's defaulting to the root window.
If all of that code belongs to a single program, you have another problem. You should always only ever create a single instance of Tk. If you need more than one window, create instances of Toplevel.
You are calling both by the name window. This means that there are two windows on the screen both acsessed by the name window. It is more conventional to use tkinter's Toplevel as follows
NewWindow = Toplevel(window)
Then, any items you want to place on this NewWindow, just use it in the place of window
MyButton = Button(NewWindow, text=hi)
As the other answer said, it is incorrect to have to Tk() in one program so you must use the Toplevel.
I was trying to create a simple app for illustration purposes. The idea is as follows:
Create an application which will run script files associated only to the selected courses (radio buttons). So, I create radio buttons which list out subjects (to click on). Once the subjects are selected the user has to hit the Enter button. This should run all the .py files for the selected subjects (execute_script function).
However, when I run my code, I get 4 messageboxes with 'None' written inside. After clicking ok on them, I get a square windows with only the enter button. What can I do to correct this problem?
def check(file_name, relStatus):
radioValue = relStatus.get()
tkMessageBox.showinfo('You checked', radioValue)
been_clicked.append(file_name)
return
def execute_script():
for name in been_cliked:
subprocess.Popen(['python', 'C:\Users\Max\Subjects\{}'.format(name)])
yield
def main():
#Create application
app = Tk()
app.title('Coursework')
app.geometry('450x300+200+200')
#Header
labelText = StringVar()
labelText.set('Select subjects')
#Dictionary with names
product_names = {}
names = []
file_name = []
names = ['Math', 'Science', 'English', 'French']
file_name = ['calc.py', 'physics.py', 'grammar.py', 'livre.py']
product_names = OrderedDict(zip(names, file_name))
#Create radio buttons
global been_clicked
been_clicked = []
relStatus = StringVar()
relStatus.set(None)
for name,file_name in product_names.iteritems():
radio1 = Radiobutton(app, text=name, value=name, \
variable=relStatus, command=check(file_name, relStatus))
button = Button(app, text='Click Here', width=20, command=execute_script())
button.pack(side='bottom', padx=15, pady=15)
app.mainloop()
if __name__ == '__main__': main()
There are a few issues with your script:
1) A typo in your execute_script() function: for name in been_cliked
2) You are actually calling the check() function when you create your radio buttons. That's why you're seeing the windows pop up when you run your program.
You need to change this:
radio1 = Radiobutton(app, text=name, value=name, \
variable=relStatus, command=check(file_name, relStatus))
to this:
radio1 = Radiobutton(app, text=name, value=name, \
variable=relStatus, command=check)
See how check no longer has brackets? That's means you're passing the function name as an argument, instead of actually calling the function. Of course, you'll see an immediate problem is that you can no longer pass arguments to your callback function! That's a bigger issue. Here's a couple links to help get you started:
How to pass an argument to event handler in tkinter?
How can I pass arguments to Tkinter button's callback command?
Here is the solution:
Change this:
command=check(file_name, reStatus)
to this:
command = lambda: check(file_name, relStatus)
3) You don't actually pack() your radio buttons anywhere. Add something like this just after you create your radio buttons in your for loop: radio1.pack(side='top')
4) You have the same problem with your callback for your Click Here button. You need to change your command to not call the function, but just refer to it: command = execute_script
5) In execute_script(), make sure you import subprocessing
6) Are you sure you want yield instead of return in your execute_script() function?
7) In all your functions, you need to make sure that been_clicked is global.
I think if you fix these issues you'll be closer to getting what you're looking for. Good luck.!