Python tkinter checkbutton value always equal to 0

2024/10/11 14:15:59

I put the checkbutton on the text widget, but everytime I select a checkbutton, the function checkbutton_value is called, and it returns 0.

Part of the code is :

def callback():file_name=askopenfilename()column_1rowname,column_name=draw_column(file_name)root = Tk()root.resizable(width=False,height=False)root.wm_title("Column")S = Scrollbar(root,orient="vertical")text=Text(root,width=15,height=10,yscrollcommand=S.set)S.config(command=text.yview)S.pack(side="right",fill="y")text.pack(side="left",fill="both",expand=True)#check the value of the checkbuttondef checkbutton_value():if(var.get()):print 1else:print 0var=BooleanVar()chk = Checkbutton(root, text=column_1rowname[1], variable=var, command=checkbutton_value)text.window_create("end", window=chk)text.config(state=DISABLED)errmsg='Error!'
Button(text='File Open',command=callback).pack(fill=X)mainloop()
Answer

The problem is that you have more than one root window. You should only ever create exactly one instance of Tk, and call mainloop exactly once. If you need additional windows, create instances of Toplevel.

Each root window (and all of its children, and all related StringVars etc.) start a new, independent tcl interpreter. Widgets and variables associated with this window can't be used in another tcl interpreter. In your case, the StringVar is associated with the first root window, but the widget is associated with the second. You can't share data between root windows like that.

https://en.xdnf.cn/q/118319.html

Related Q&A

How does derived class arguments work in Python?

I am having difficulty understanding one thing in Python.I have been coding in Python from a very long time but theres is something that just struck me today which i struggle to understandSo the situat…

grouping on tems in a list in python

I have 60 records with a column "skillsList" "("skillsList" is a list of skills) and "IdNo". I want to find out how many "IdNos" have a skill in common.How …

How do I show a suffix to user input in Python?

I want a percentage sign to display after the users enters their number. Thankspercent_tip = float(input(" Please Enter the percent of the tip:")("%"))For example, before the user t…

Discord.py Self Bot using rewrite

Im trying to make a selfbot using discord.py rewrite. Im encountering issues when attempting to create a simple command. Id like my selfbot to respond with "oof" when ">>>test&q…

int to binary python

This question is probably very easy for most of you, but i cant find the answer so far.Im building a network packet generator that goes like this:class PacketHeader(Packet): fields = OrderedDict([(&quo…

Get aiohttp results as string

Im trying to get data from a website using async in python. As an example I used this code (under A Better Coroutine Example): https://www.blog.pythonlibrary.org/2016/07/26/python-3-an-intro-to-asyncio…

Waiting for a timer to terminate before continuing running the code

The following code updates the text of a button every second after the START button was pressed. The intended functionality is for the code to wait until the timer has stopped before continuing on with…

PySpark: how to resolve path of a resource file present inside the dependency zip file

I have a mapPartitions on an RDD and within each partition, a resource file has to be opened. This module that contains the method invoked by mapPartitions and the resource file is passed on to each ex…

Convert normal Python script to REST API

Here I have an excel to pdf conversion script. How can I modify it to act as a REST API? import os import comtypes.client SOURCE_DIR = D:/projects/python TARGET_DIR = D:/projects/python app = comtypes…

How to track changes in specific registry key or file with Python? [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …