Posts: 84
Threads: 24
Joined: Aug 2020
I have a window with a check box. When I click it I get the result I expect. But when I uncheck it I don't get what I expect. Not sure why. Have googled and watched videos for 2 hours.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
def show_select1():
original_horse = horses_name[ 0 ]
if namevar1.get() = = 'scratched' :
horses_name[ 0 ] = 'scratched'
if namevar1.get() = = 'not scratched' :
horses_name[ 0 ] = original_horse
print (horses_name)
if horse_count > 0 :
c1 = Checkbutton(scratches, text = horse_saddle[ 0 ] , variable = namevar1, onvalue = 'scratched' , offvalue = 'not scratched' , command = show_select1 )
c1.deselect()
c1.place(x = 5 , y = 50 )
my_scratch_label2 = Label(scratches, text = horses_name[ 0 ], fg = "black" , font = ( "sans_serif" , 10 , 'bold' )).place(x = 55 , y = 50 )
|
I check the box and this works.... horses_name[0] = 'scratched' But when I uncheck it immediately the horses_name[0] remains 'scratched'. I don't understand why.
Posts: 8,156
Threads: 160
Joined: Sep 2016
Sep-18-2020, 09:31 AM
(This post was last modified: Sep-18-2020, 09:31 AM by buran.)
When you uncheck it, the original name is "checked" (as set at the time you check it).
Posts: 6,780
Threads: 20
Joined: Feb 2020
Checkbox works fine. Program logic is the problem.
Scratched should not change the name of the horse, it should change the state of the horse. "Racy Lacy" is either running the race or she is scratched. If she is scratched her name is still "Racy Lacy". The way your program works you rename "Racy Lacy" to "scratched" if you press the checkbox (you set horse_name[0] to "scratched"). Now "Racy Lacy" is no longer on record for being in this race and can never be un-scratched.
I don't see any need for a checkbox callback function at all. The checkbox saves the state of scratched. The stat of scratched is always available by checking the value of namevar1. The scratched state is only important when you open betting or whatever, so you some other control, maybe an Accept button, would call a function that checks the scratched state for all the horses at the same time.
You should hardly ever hard code an index (horses_name[0]) or have a variable like namevar1. There are multiple horses in the race and your code acknowledges this by having horses_name being a list. So why isn't namevar (should be called scratched) also a list? If scratched is a list you can treat all horses generically and you will no longer have functions like show_select1() that only works for one horse and requires multiple copies to work for many horses.
Posts: 84
Threads: 24
Joined: Aug 2020
Thank You. I posted just the one checkbox. I have up to 25 checkboxes. I wanted the user to be able to uncheck a horse if he checked it by mistake. I think I under stand the need for a second ;ist of checked status. Cause after the scratches are done, I have to use horses data to create race factors to ultimately get final ratings. I suppose I would then use the scratched list to check to see if horse is running or not before I create the ratings.
Posts: 84
Threads: 24
Joined: Aug 2020
I changed the code to the following and No matter what I do, the variable will not change from "not scratched" to "scratched"
1 2 3 4 5 6 7 8 9 |
if horse_count > 0 :
c1 = Checkbutton(scratches, text = horse_saddle[ 0 ] , variable = namevar1, onvalue = 'scratched' , offvalue = 'not scratched' )
c1.deselect()
c1.place(x = 5 , y = 50 )
my_scratch_label2 = Label(scratches, text = horses_name[ 0 ], fg = "black" , font = ( "sans_serif" , 10 , 'bold' )).place(x = 55 , y = 50 )
if namevar1.get() = = 'scratched' :
scratched_list.insert( 0 , 1 )
|
What is wrong? Driving me crazy.
Posts: 1,583
Threads: 3
Joined: Mar 2020
The only place you look at the variable in the above code is immediately after the button is created (it can't be checked yet).
Do you have something watching that variable or that checkbutton to do a task when the state changes?
Posts: 84
Threads: 24
Joined: Aug 2020
Sep-19-2020, 01:43 AM
(This post was last modified: Sep-19-2020, 02:10 AM by Milfredo.)
I did. Had a function. But I was told I shouldn't have to have the extra step. I will go back to the function and see how that works. Thanks for the insight.
Ok. I rewrote the code a bit. Still can not get the variable to show "scratched".
1 2 3 4 5 6 7 8 9 10 11 |
def scratch_one():
if namevar1.get() = = 'scratched' :
scratched_list.insert( 0 , 1 )
if horse_count > 0 :
c1 = Checkbutton(scratches, text = horse_saddle[ 0 ] , variable = namevar1, onvalue = 'scratched' , offvalue = 'not scratched' , command = scratch_one )
c1.deselect()
c1.place(x = 5 , y = 50 )
my_scratch_label2 = Label(scratches, text = horses_name[ 0 ], fg = "black" , font = ( "sans_serif" , 10 , 'bold' )).place(x = 55 , y = 50 )
|
Now what am I doing wrong?
Posts: 6,780
Threads: 20
Joined: Feb 2020
Sep-19-2020, 03:53 AM
(This post was last modified: Sep-19-2020, 03:53 AM by deanhystad.)
If the box is checked, namevar1.get() will return "scratched", if not it will return "not scratched". What do you want to do with that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from tkinter import *
def scratch_one():
print (scratched.get())
root = Tk()
scratched = StringVar()
cb = Checkbutton(root, text = 'HiHo' ,
variable = scratched, onvalue = 'scratched' , offvalue = 'not scratched' ,
command = scratch_one )
cb.place(x = 5 , y = 50 )
mainloop()
|
Output: scratched
not scratched
scratched
not scratched
If you are making some kind of form there likely is no reason to specify a command for the scratched checkbutton. When you need the scratched value you would use the variable.get() function to get the current value.
Posts: 84
Threads: 24
Joined: Aug 2020
Thank you Dean. When check box clicked choosing "scratched" I then want to put a 1 into a list. Then after scratches done, I will cycle through the list, and if horse not scratched, then I will get their data and do some calculations on that data. If 1 is in the list corresponding to a horse, then their data will be skipped and not used in the calculations. Here is how I am attempting to do that.
1 2 3 4 |
def scratch_one():
print (namevar1.get())
if namevar1.get() = = 'scratched' :
scratched_list.insert( 0 , 1 )
|
But the insert doesn't work. Problem with if statement?
Posts: 1,583
Threads: 3
Joined: Mar 2020
What are you going to do if they unclick the box? You'd have to remove the inserted 1, I presume. Do you do that?
|