I have some problems with Checkbutton in classes. They always return the start value. I am attaching the example code, in Main.py I create a window and a button to invoke my class with the checkbutton. In this second class (WindowProteins.py:) the check button does not work and always returns the same value
Main.py:
#tkinter import
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
from tkinter.messagebox import showinfo
import WindowProteins as wPr
#font
font_title = ('times', 18, 'bold')
font_subtitle = ('times', 14, 'bold')
def CreateProteins():
windowPr = wPr.ProteinsWindow()
windowPr.mainloop()
#create welcome window
def CreateWelcome():
#window
global window_welcome
window_welcome = tk.Tk()
window_welcome.geometry("400x300") # Size of the window
window_welcome.title('Main')
#button
btn_proteins = tk.Button(window_welcome, text='Proteins',
width=20,command = lambda:CreateProteins())
btn_proteins.grid(row=2,column=1)
window_welcome.mainloop() #Keep the window open
CreateWelcome()
WindowProteins.py:
#tkinter import
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
from tkinter.messagebox import showinfo
class ProteinsWindow(tk.Tk):
df = pd.DataFrame()
def __init__(self):
super().__init__()
# configure the root window
self.title('Proteins')
self.geometry('800x400')
#fonts
self.font_title=('times', 18, 'bold')
self.font_subtitle = ('times', 14, 'bold')
self.font_base = ('times', 11)
#Protein FDR checkboxes
self.var_chc_low = IntVar(value=1)
self.chc_low = tk.Checkbutton(self, text='Low',variable=self.var_chc_low, onvalue=1, offvalue=0, command=self.agreement_changed )
self.chc_low.grid(row=0,column=0, sticky='w')
self.chc_low.select()
def agreement_changed(self):
print(str(self.var_chc_low.get()))
how can i solve?
Tk()here - an explicit one inCreateWelcome(), and as the base class ofProteinsWindow. One of the numerous problems caused by this is that Tkinter Vars don't work right. UseToplevelinstead as the base class for your second window.tk.Tk()only to create main window. Other windows you should create usingtk.Toplevel(). And you should use only onemainloop()because two loops can make conflict when you try to get value.command=CreateProteinswithoutlambdaand without()(and without spaces around=- see more PEP 8 -- Style Guide for Python Code )