Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checkbox half working
#1
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.

#PROCESS SCRATCH BOX SELECTION
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.
Reply
#2
When you uncheck it, the original name is "checked" (as set at the time you check it).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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.
Reply
#5
I changed the code to the following and No matter what I do, the variable will not change from "not scratched" to "scratched"

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.
Reply
#6
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?
Reply
#7
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".

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?
Reply
#8
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?
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.
Reply
#9
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.

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?
Reply
#10
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checkbox itens with a button to run action Woogmoog 3 945 Dec-19-2022, 11:54 AM
Last Post: Woogmoog
  Use String contents for checkbox name jcday 1 1,621 Dec-06-2019, 06:01 PM
Last Post: Larz60+
  How to run a loop till half of the string...without using inbuilt function krish 1 2,910 Sep-28-2017, 05:34 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020