Python 3.4 tkinter checkbutton processing variable not working / responding

EDIT Now it has moved to Python 3.4.3 tkinter - the program freezes when declaring IntVar or any other tkinter data type , since this is the root of the problem that is now resolved. Basically, NEVER call ANYTHING "master" in Python 3.x using tkinter, it causes an endless loop :( My complete code is now deleted, as this is my term paper and doesn’t want people to pinch it: D EDIT

I am relatively new to tkinter and can't see where I am going wrong. I tried using StringVar () and a regular string to disable multiple input fields when the Checkbutton function is enabled. Here is the frame in which the problem is:

class CActivity(tk.Frame):

def Clear(self):
    pass # To be completed

def Today(self):
    if self.todayVar == "ON":
        self.day.configure(state="disabled")
        self.month.configure(state="disabled")
        self.year.configure(state="disabled")
    else:
        self.day.configure(state="normal")
        self.month.configure(state="normal")
        self.year.configure(state="normal")

def createWidgets(self):

    self.title = tk.Label(self)
    self.title["text"] = "Add an Activity"
    self.title["font"] = ("Times New Roman",30)
    self.title["fg"] = "purple"
    self.title.grid(row=0,column=0,sticky="W",padx=5,pady=5)

    self.todayVar = ""

    tk.Label(self,text="Activity Name:",font=("Times New Roman",15)).grid(row=1,column=0,sticky="W",padx=5,pady=5)
    name = tk.Entry(self).grid(row=1,column=1,columnspan=3,sticky="E",padx=5,pady=5)
    tk.Label(self,text="Priority:",font=("Times New Roman",15)).grid(row=2,column=0,sticky="W",padx=5,pady=5)
    priority = tk.Checkbutton(self).grid(row=2,column=1,sticky="W",padx=0,pady=5)
    tk.Label(self,text="Today?",font=("Times New Roman",15)).grid(row=3,column=0,sticky="W",padx=5,pady=5)
    today = tk.Checkbutton(self,onvalue="ON",offvalue="OFF",variable=self.todayVar,command=self.Today).grid(row=3,column=1,sticky="W",padx=0,pady=5) #problem possibly on this line
    tk.Label(self,text="Date (DD/MM/YYYY):",font=("Times New Roman",15)).grid(row=4,column=0,sticky="W",padx=5,pady=5)

    day = tk.Entry(self,width=2).grid(row=4,column=1,sticky="W",padx=2,pady=5)
    month = tk.Entry(self,width=2).grid(row=4,column=2,sticky="W",padx=2,pady=5)
    year = tk.Entry(self,width=4).grid(row=4,column=3,sticky="W",padx=2,pady=5)

    self.clear = tk.Button(self, command=self.Clear)
    self.clear["text"] = "Clear"
    self.clear["font"] = ("Times New Roman",15)
    self.clear["fg"] = "red"
    self.clear.grid(row=7,column=4,sticky="WE",padx=5,pady=5)

    self.back = tk.Button(self)
    self.back["text"] = "Back"
    self.back["font"] = ("Times New Roman",15)
    self.back["fg"] = "red"
    self.back["command"] = self.parent.Menu
    self.back.grid(row=8,column=4,sticky="WE",padx=5,pady=5)

def __init__(self, parent):

    tk.Frame.__init__(self, parent)
    self.pack()
    self.parent = parent
    self.createWidgets()

And here instead of StringVar () instead of the standard Python string:

class CActivity(tk.Frame):

def Clear(self):
    pass # To be completed

def Today(self):
    if self.todayVar.get() == "ON":
        self.day.configure(state="disabled")
        self.month.configure(state="disabled")
        self.year.configure(state="disabled")
    else:
        self.day.configure(state="normal")
        self.month.configure(state="normal")
        self.year.configure(state="normal")

def createWidgets(self):

    self.title = tk.Label(self)
    self.title["text"] = "Add an Activity"
    self.title["font"] = ("Times New Roman",30)
    self.title["fg"] = "purple"
    self.title.grid(row=0,column=0,sticky="W",padx=5,pady=5)

    self.todayVar = tk.StringVar()

    tk.Label(self,text="Activity Name:",font=("Times New Roman",15)).grid(row=1,column=0,sticky="W",padx=5,pady=5)
    name = tk.Entry(self).grid(row=1,column=1,columnspan=3,sticky="E",padx=5,pady=5)
    tk.Label(self,text="Priority:",font=("Times New Roman",15)).grid(row=2,column=0,sticky="W",padx=5,pady=5)
    priority = tk.Checkbutton(self).grid(row=2,column=1,sticky="W",padx=0,pady=5)
    tk.Label(self,text="Today?",font=("Times New Roman",15)).grid(row=3,column=0,sticky="W",padx=5,pady=5)
    today = tk.Checkbutton(self,onvalue="ON",offvalue="OFF",variable=self.todayVar,command=self.Today).grid(row=3,column=1,sticky="W",padx=0,pady=5)
    tk.Label(self,text="Date (DD/MM/YYYY):",font=("Times New Roman",15)).grid(row=4,column=0,sticky="W",padx=5,pady=5)

    day = tk.Entry(self,width=2).grid(row=4,column=1,sticky="W",padx=2,pady=5)
    month = tk.Entry(self,width=2).grid(row=4,column=2,sticky="W",padx=2,pady=5)
    year = tk.Entry(self,width=4).grid(row=4,column=3,sticky="W",padx=2,pady=5)

    self.clear = tk.Button(self, command=self.Clear)
    self.clear["text"] = "Clear"
    self.clear["font"] = ("Times New Roman",15)
    self.clear["fg"] = "red"
    self.clear.grid(row=7,column=4,sticky="WE",padx=5,pady=5)

    self.back = tk.Button(self)
    self.back["text"] = "Back"
    self.back["font"] = ("Times New Roman",15)
    self.back["fg"] = "red"
    self.back["command"] = self.parent.Menu
    self.back.grid(row=8,column=4,sticky="WE",padx=5,pady=5)

def __init__(self, parent):

    tk.Frame.__init__(self, parent)
    self.pack()
    self.parent = parent
    self.createWidgets()

, "Checkbutton", "Checkbutton" , . StringVar() tk - . , , , .

0
2

, :

#!/usr/bin/env python

''' Toggle disable / normal of Tkinter widgets

    Written by PM 2Ring & R. Murray 2015.11.15
    See http://stackoverflow.com/q/33711472/4014959
'''

#Python 3 / Python 2 Tkinter import
try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk


class CActivity(tk.Frame):
    def today_cb(self):
        if self.todayVar.get():
            state = "disabled"
        else:
            state = "normal"
        #print(state)

        self.day.configure(state=state)
        self.month.configure(state=state)
        self.year.configure(state=state)


    def create_widgets(self):
        title_text = ("Click the 'Today' Checkbutton to\n"
            "disable / enable date Entry widgets")
        title = tk.Label(self, text=title_text)
        title.grid(row=0, column=0, sticky="W", padx=5, pady=5)

        self.todayVar = tk.IntVar()

        tk.Label(self,text="Today").grid(row=1, column=1, 
            sticky="W", padx=0, pady=5)
        today = tk.Checkbutton(self, variable=self.todayVar, 
            command=self.today_cb)
        today.grid(row=2, column=1, sticky="W", padx=0, pady=5)

        #Date Entry widgets
        tk.Label(self,text="Day").grid(row=3, column=1,
            sticky="W", padx=2, pady=5)
        self.day = tk.Entry(self, width=2)
        self.day.grid(row=4, column=1, sticky="W", padx=2, pady=5)

        tk.Label(self,text="Month").grid(row=3, column=2,
            sticky="W", padx=2,pady=5)
        self.month = tk.Entry(self, width=2)
        self.month.grid(row=4, column=2, sticky="W", padx=2, pady=5)

        tk.Label(self,text="Year").grid(row=3, column=3,
            sticky="W", padx=2, pady=5)
        self.year = tk.Entry(self, width=4)
        self.year.grid(row=4, column=3, sticky="W", padx=2, pady=5)


    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.pack()
        self.parent = parent
        self.create_widgets()


if __name__ == '__main__':
    master = tk.Tk()
    master.title("Disable widgets demo")
    frame = CActivity(master)
    master.mainloop()

.

createWidgets Entry , day, , self.day, Checkbutton.

, .grid , . None,

day = tk.Entry(self,width=2).grid(row=4,column=1)

day None.

.grid ( .pack) , , , , ,:)

Checkbutton, . onvalue="ON",offvalue="OFF" Checkbutton, 0 1 , IMHO ( , :)), IntVar, StringVar.

, PEP 8.


. day - , self.day, , ,

day = tk.Entry(self,width=2)
self.day.grid(row=4,column=1,sticky="W",padx=2,pady=5)

self.day = tk.Entry(self,width=2)
self.day.grid(row=4,column=1,sticky="W",padx=2,pady=5)

, variable textvariable, Tkinter StringVar, IntVar ..

0

variable textvariable Tkinter StringVar, IntVar, DoubleVar BooleanVar. .

-, get , if.

self.todayVar = StringVar()
...
today = tk.Checkbutton(..., variable=self.todayVar, ...)
...
if self.todayVar.get() == "ON":
0

Source: https://habr.com/ru/post/1615919/


All Articles