简体   繁体   中英

Tkinter button bind issues

My goal is to eventually have a few labels and entries that appear and correspond with the different days of the week. I am currently working on getting the button binding to generate a different label depending on which day of the week is selected. It's sort of working, but not how I expected and not how I really want. Right now I have to click on a button twice for it to update the label. I'm still pretty new to programming and I appreciate any help you can provide!

root = Tk()
root.geometry('300x300')
root.title('Day')

frame3 = Frame(root)
frame4 = Frame(root)
frame3.grid()

dayFrame = Frame(frame4)
class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.var1 = StringVar()
        self.var1.set('')
        self.sunBtn = Button(frame3, text='Sunday', command=lambda day='Sunday': self.var1.set(day))
        self.sunBtn.bind('<Button-1>', self.openFrame)
        self.sunBtn.grid(row=1, column=0)
        self.monBtn = Button(frame3, text='Monday', command=lambda day='Monday': self.var1.set(day))
        self.monBtn.bind('<Button-1>', self.openFrame)
        self.monBtn.grid(row=1, column=1)

    def openFrame(self,*args):
        frame4.grid()
        dayFrame.grid()
        dayLbl = Label(dayFrame, text=self.var1.get())
        dayLbl.grid(row=1, column=0)

app = App(root)
app.mainloop()

UPDATE: I realized my mistake. I feel dumb. I replaced

self.var1.set('')

with

self.var1.trace('w', self.openFrame)

and it seems like everything is working properly. Hopefully this can help another beginner at some point!

Don't use bind and command at the same time: (and don't forget to write your import line)

from tkinter import *

root = Tk()
root.geometry('300x300')
root.title('Day')

frame3 = Frame(root)
frame4 = Frame(root)
frame3.grid()

dayFrame = Frame(frame4)
class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.var1 = StringVar()
        self.var1.set('')
        self.sunBtn = Button(frame3, text='Sunday', command= lambda: self.openFrame('Sunday'))
        self.sunBtn.grid(row=1, column=0)
        self.monBtn = Button(frame3, text='Monday', command=lambda: self.openFrame('Monday'))
        self.monBtn.grid(row=1, column=1)

    def openFrame(self, day):
        frame4.grid()
        dayFrame.grid()
        dayLbl = Label(dayFrame, text=day)
        dayLbl.grid(row=1, column=0)

app = App(root)
app.mainloop()

A very different (and probably better) solution:

from tkinter import *
import calendar

root = Tk()
root.geometry('600x300')
root.title('Day')

frame3 = Frame(root)
frame4 = Frame(root)
frame3.grid()

dayFrame = Frame(frame4)
class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)        
        frame4.grid()
        dayFrame.grid()
        self.dayLbl = Label(dayFrame, text="")
        self.dayLbl.grid(row=1, column=0)
        for i,  day in enumerate(calendar.day_name):
            button = Button(frame3, text=day, command= lambda day=day:\
                self.dayLbl.configure(text=day))
            button.grid(row=1, column=i)         

app = App(root)
app.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM