python - Using tkinter to assign a global variable and destroy the gui -


i have been writing annual data verification program , need bit of user input , decided go tkinter route. have created interface 1 of user input screen, , have make others, i'm having issues destruction of windows after selection made, , globalization of variable.

so ideally program run, window pops up, appropriate attribute selection made, text on button passed "assign" function, creates global variable used in program, , window disappears.

as stands now, running of code results in error: "tclerror: can't invoke "button" command: application has been destroyed".

if comment out "mgui.destroy()" line, can select button , close window manually, "drn" variable passed variable "x" no matter what!

import sys tkinter import *  def assign(value):     global x     x = value     mgui.destroy()  mgui = tk() mgui.geometry("500x100+500+300") mgui.title("attribute selection window")  mlabel = label(mgui, text = "please select 1 of following attributes assign selected convwks feature:").pack()  mbutton = button(mgui, text = "con", command = assign("con")).pack() mbutton = button(mgui, text = "ms", command = assign("ms")).pack() mbutton = button(mgui, text = "drn", command = assign("drn")).pack()  mgui.mainloop()     #for windows 

bonus problem: putting of buttons on same row spaces in between them, while keeping them centered.

the problem code can't call functions when adding button commands. can't write button(command=function()), have write button(command=function). if want pass argument function, you'll have this:

instead of:

mbutton = button(mgui, text = "con", command = assign("con")).pack() mbutton = button(mgui, text = "ms", command = assign("ms")).pack() mbutton = button(mgui, text = "drn", command = assign("drn")).pack() 

you have write:

mbutton = button(mgui, text = "con", command = lambda: assign("con")).pack() mbutton = button(mgui, text = "ms", command = lambda: assign("ms")).pack() mbutton = button(mgui, text = "drn", command = lambda: assign("drn")).pack() 

if want put buttons on same row, use code:

import sys tkinter import *  def assign(value):     global x     x = value     mgui.destroy()  mgui = tk() mgui.geometry("500x100+500+300") mgui.title("attribute selection window")  frame1 = frame(mgui) frame1.pack()  mlabel = label(frame1, text = "please select 1 of following attributes assign selected convwks feature:").grid(row=0, column=0)  frame2 = frame(mgui) frame2.pack()   mbutton = button(frame2, text = "con", command = lambda: assign("con")).grid(row=0, column=0, padx=10) mbutton = button(frame2, text = "ms", command = lambda: assign("ms")).grid(row=0, column=1, padx=10) mbutton = button(frame2, text = "drn", command = lambda: assign("drn")).grid(row=0, column=2, padx=10) mgui.mainloop()     #for windows 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -