How do I find out if I have pressed the checkbutton? - python-3.x

from Tkinter import *
window = Tk()
window.config(background="green")
window.bind("<Escape>", quit)
cbttn = Checkbutton(text="Caps?").grid(row=3, column=0)
Does if cbttn = True work for it? Or do I have to move the .grid() function and move it to the next line of code.

An assignment such as:
cbttn = Checkbutton(text="Caps?").grid(row=3, column=0)
yields in cbttn being of an object of None type.
Either remove the assignment to cbttn (if you do not want to reference it further in the script)
Checkbutton(text="Caps?").grid(row=3, column=0)
or move grid to a new line as:
cbttn = Checkbutton(text="Caps?")
cbttn.grid(row=3, column=0)
And to see if the Checkbutton was pressed or not, use the command option available. Check the example here. Taking this example:
from tkinter import *
def display():
print(CheckVar1.get())
top = Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height=5, \
width = 20, command = display)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height=5, \
width = 20)
C1.pack()
C2.pack()
top.mainloop()
The output should keep switching between 0 and 1

Related

tkinter key binding causes image to disappear

I'm trying to bind arrow keys to buttons or at least the functions of the buttons (button_forward and button_back). The function of the buttons works, however, when I bind a key to the button the image just disappears.
It would also be awesome if someone could help me figure out how to create a loop to define the images and put them into a list. I'm just so lost when it comes to that.
The main purpose of the code is to be an image viewer that flashes an LED strip when an image changes.
I want to be able to control it using arrow keys to move forward and back between the images.
from tkinter import Tk, Button,Label, DISABLED
from PIL import ImageTk,Image
import board
import time
import neopixel
pixels = neopixel.NeoPixel(board.D18, 30)
root= Tk()
root.configure(bg='black')
root.title("please work")
# define, load, show
my_img1 = ImageTk.PhotoImage(Image.open("1.bmp"))
my_img2 = ImageTk.PhotoImage(Image.open("2.bmp"))
my_img3 = ImageTk.PhotoImage(Image.open("3.bmp"))
my_img4 = ImageTk.PhotoImage(Image.open("4.bmp"))
my_img5 = ImageTk.PhotoImage(Image.open("5.bmp"))
my_img6 = ImageTk.PhotoImage(Image.open("6.bmp"))
my_img7 = ImageTk.PhotoImage(Image.open("7.bmp"))
my_img8 = ImageTk.PhotoImage(Image.open("8.bmp"))
image_list = [my_img1, my_img2, my_img3, my_img4, my_img5, my_img6, my_img7, my_img8]
my_label = Label(image=my_img1)
my_label.grid(row = 0, column = 0, columnspan= 3, rowspan = 25, padx=440, pady= 5)
def forward(image_number, event = None):
global my_label
global button_forward
global button_back
my_label.grid_forget()
my_label = Label(image = image_list[image_number-1])
button_forward = Button(root, text = "next", command=lambda: forward(image_number+1))
button_back = Button(root, text = "previous", command = lambda: back(image_number-1))
if image_number == 7:
button_forward = Button(root, text = "next", state = DISABLED)
my_label.grid(row = 0, column = 0, columnspan = 3, rowspan = 25, padx=440, pady= 5)
button_back.grid(row = 23, column = 0)
button_forward.grid(row = 23, column = 2)
pixels.fill((255,0,0))
time.sleep(0.1)
pixels.fill((0,0,0))
time.sleep(0.5)
def back(image_number,):
global my_label
global button_forward
global button_back
my_label.grid_forget()
my_label = Label(image = image_list[image_number-1])
button_forward = Button(root, text = "next", command=lambda: forward(image_number+1))
button_back = Button(root, text = "previous", command = lambda: back(image_number-1))
my_label.grid(row = 0, column = 0, columnspan = 3, rowspan = 25, padx=440, pady= 5)
if image_number == 1:
button_back = Button(root, text = "previous", state = DISABLED)
button_back.grid(row=23, column = 0 )
button_exit.grid(row=23, column = 1 )
button_forward.grid(row=23, column = 2)
pixels.fill((255,0,0))
time.sleep(0.1)
pixels.fill((0,0,0))
time.sleep(0.5)
button_back = Button(root, text = "previous", command = back)
button_exit = Button(root, text = "Exit", command = root.quit)
button_forward = Button(root, text = "next", command =lambda:forward(2))
root.bind('<Left>', back)
root.bind('<Right>', forward)
button_back.grid(row=23, column = 0 )
button_exit.grid(row=23, column = 1 )
button_forward.grid(row=23, column = 2)
root.mainloop()
Your bindings cannot work because the event corresponding to the keypress will be passed instead of the image_number argument in back() and forward().
Also, you are recreating the widgets every time while you should only reconfigure them. So to change the label's image, you can do: my_label.configure(image=<new image>). Also instead of passing image_number as an argument, I would rather use a global variable, so that it will be easier to use the functions in the key bindings:
from tkinter import Tk, Button, Label, DISABLED, NORMAL
from PIL import ImageTk, Image
import board
import time
import neopixel
pixels = neopixel.NeoPixel(board.D18, 30)
root = Tk()
root.configure(bg='black')
root.title("please work")
# define, load, show
my_img1 = ImageTk.PhotoImage(Image.open("1.bmp"))
my_img2 = ImageTk.PhotoImage(Image.open("2.bmp"))
my_img3 = ImageTk.PhotoImage(Image.open("3.bmp"))
my_img4 = ImageTk.PhotoImage(Image.open("4.bmp"))
my_img5 = ImageTk.PhotoImage(Image.open("5.bmp"))
my_img6 = ImageTk.PhotoImage(Image.open("6.bmp"))
my_img7 = ImageTk.PhotoImage(Image.open("7.bmp"))
my_img8 = ImageTk.PhotoImage(Image.open("8.bmp"))
image_list = [my_img1, my_img2, my_img3, my_img4, my_img5, my_img6, my_img7, my_img8]
image_number = 1
# create the label only once
my_label = Label(root, image=image_list[image_number - 1])
my_label.grid(row=0, column=0, columnspan= 3, rowspan=25, padx=440, pady= 5)
def forward(event=None):
global image_number # global variable to keep track of the displayed image
image_number += 1
# change image in label
my_label.configure(image=image_list[image_number - 1])
if image_number == 8:
# last image, disable forward button
button_forward.configure(state=DISABLED)
elif image_number == 2:
# no longer first image, re-enable back button
button_back.configure(state=NORMAL)
pixels.fill((255,0,0))
time.sleep(0.1)
pixels.fill((0,0,0))
time.sleep(0.5)
def back(event=None):
global image_number
image_number -= 1
my_label.configure(image=image_list[image_number - 1])
if image_number == 1:
# first image, disable back button
button_back.configure(state=DISABLED)
elif image_number == 7:
# no longer last image, re-enable forward button
button_forward.configure(state=NORMAL)
pixels.fill((255,0,0))
time.sleep(0.1)
pixels.fill((0,0,0))
time.sleep(0.5)
button_back = Button(root, text="previous", command=back, state=DISABLED)
button_exit = Button(root, text="Exit", command=root.quit)
button_forward = Button(root, text="next", command=forward)
root.bind('<Left>', back)
root.bind('<Right>', forward)
button_back.grid(row=23, column=0)
button_exit.grid(row=23, column=1)
button_forward.grid(row=23, column=2)
root.mainloop()

Python tkinter - label not showing on the 2nd screen

I created a code with a yes/no question, and if yes, I use an entry box to ask how many. But when I reach to that How many question, the label is not showing and I don't understand why?
Thanks in advance, below is the code:
from tkinter import filedialog, messagebox, ttk, constants
from tkinter import *
root = Tk()
root.focus_force()
root.withdraw()
root.call('wm', 'attributes', '.', '-topmost', True)
yesnob = messagebox.askyesno('Test','Do you have a clue?')
if yesnob == True:
root2 = Tk()
root2.call('wm', 'attributes', '.', '-topmost', True)
root2.wm_title('How many ?')
nb_b = 0
title_loop = Label(root2, textvariable = 'How many ?', height = 2, width = 15)
title_loop.grid(row = 1, column = 0)
entrybox = Entry(root2, textvariable = nb_b, width = 5)
entrybox.grid(row = 2, column = 0)
def get_data():
global nb_b
try:
nb_b = int((entrybox.get()))
except ValueError:
no_int = messagebox.showerror('Error', 'You did not enter a number, try again!')
root.destroy()
root2.destroy()
exit_but = Button(root2, text = 'OK', command = get_data, height = 3, width = 5)
exit_but.grid(row = 3, column = 1)
root2.mainloop()
else:
root.destroy()
root.mainloop()
Changing the "textvariable" to "text" worked for me:
title_loop = Label(root2, text = 'How many ?', height = 2, width = 15)
You created the Label with the textvariable argument. If you change it to text the label is shown:
title_loop = Label(root2, text= 'How many ?', height = 2, width = 15)
textvariable can be used in combination with a StringVar if you want to have a text that can be changed. If the text is static use the text argument.

Python - tkinter interface, using bind works only the first time (when running the program)

I'm attaching the code below.
There is an event on "focus out", the event triggers once when the code is initialized. Afterwards focusing out of the Spinbox does not trigger the event anymore. The question is, why? And, how do I fix that?
code :
import tkinter as tk
import os, time
def spinBoxValidateRange(widget_name):
print(str(widget_name.winfo_name()) + " focus out")
# Making a window, and giving it some settings
root = tk.Tk()
root.resizable(False, False)
root.winfo_toplevel().title("Test")
# creating a user GUI
default_pady = 2
default_padx = 5
sbx_max_img_width = tk.Spinbox(from_=500, to=5000, width = 4)
sbx_max_img_width.delete(0, 'end')
sbx_max_img_width.insert(0, 1000)
sbx_max_img_height = tk.Spinbox(from_=500, to=5000, width = 4)
sbx_max_img_height.delete(0, 'end')
sbx_max_img_height.insert(0, 1000)
sbx_max_img_width.grid(row = 0, column = 1, sticky = "W", pady = default_pady, padx = default_padx)
sbx_max_img_height.grid(row = 0, column = 3, sticky = "W", pady = default_pady, padx = default_padx)
sbx_max_img_width.bind("<FocusOut>", spinBoxValidateRange(sbx_max_img_width))
sbx_max_img_height.bind("<FocusOut>", spinBoxValidateRange(sbx_max_img_height))
root.mainloop()
Here is the corrected code (that works) -
import tkinter as tk
import os, time
def spinBoxValidateRange(some_widget):
print(str(some_widget.widget.winfo_name()) + " focus out")
# Making a window, and giving it some settings
root = tk.Tk()
root.resizable(False, False)
root.winfo_toplevel().title("Test")
# creating a user GUI
default_pady = 2
default_padx = 5
sbx_max_img_width = tk.Spinbox(from_=500, to=5000, width = 4)
sbx_max_img_width.delete(0, 'end')
sbx_max_img_width.insert(0, 1000)
sbx_max_img_height = tk.Spinbox(from_=500, to=5000, width = 4)
sbx_max_img_height.delete(0, 'end')
sbx_max_img_height.insert(0, 1000)
sbx_max_img_width.grid(row = 0, column = 1, sticky = "W", pady = default_pady, padx = default_padx)
sbx_max_img_height.grid(row = 0, column = 3, sticky = "W", pady = default_pady, padx = default_padx)
sbx_max_img_width.bind("<FocusOut>", lambda parameter = sbx_max_img_width: spinBoxValidateRange(parameter))
sbx_max_img_height.bind("<FocusOut>", lambda parameter = sbx_max_img_height: spinBoxValidateRange(parameter))
root.mainloop()

Trouble with place_forget()

I have been trying to get .place_forget() to work in my test application but I am having some difficulty.
I am trying to get the widgets in "def select_1:" to show when R1 "Yes" checkbox is checked and then have the widgets not show when the check is removed.
The widgets show when I check the box but will not forget(hide) when it is then unchecked.
Any assistance would be greatly appreciated.
import os, sys
import pywintypes
from tkinter import *
#================================= MAIN WINDOW =================================
root = Tk()
w = 750
h = 325
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.title('Test Application')
root.geometry('750x325')
root.config(bg='#000000')
pass_entry=StringVar()
pass_1=StringVar()
var1 = IntVar()
var2 = IntVar()
selection = StringVar()
#=================================== HEADER ====================================
header = Label(root, font = ('Times New Roman','25','bold'), text='Test Application',
bg = ('#000000'), fg = ('#B3CDE0'))
header.place(x=250,y=15)
#================================ RESULT WINDOW ================================
result_window = Text(root, width = 80, height = 10)
result_window.place(x=50, y=75)
#=========================== CHECK BUTTON SELECTION ============================
def select_1():
if var1.get()==1:
pass_entry = Entry(root, width = 20, textvariable = pass_1, bg='#000000', fg='#B3CDE0')
pass_entry.place(x=250,y=275)
pass_entry.focus_set()
selection = Label(root, text='Enter OS Password:', bg='#000000', fg='#B3CDE0')
selection.place(x=140,y=275)
elif var1.get()==0:
pass_entry.place_forget()
selection.place_forget()
else:
return
#========================= ACCESS CREDENTIAL MANAGER ===========================
def getpass():
if var1.get()==1:
os.system('explorer.exe')
else:
return
def close():
root.destroy()
#=============================== RADIO BUTTONS =================================
R1 = Checkbutton(root, text="Yes", variable = var1, onvalue = 1, offvalue = 0, height=1, width = 15, activebackground = '#000000', bg='#000000', fg='#B3CDE0', command=select_1)
R1.place(x=350,y=250)
R2 = Checkbutton(root, text="No ", variable = var2, onvalue = 1, offvalue = 0, height=1, width = 15, bg='#000000', fg='#B3CDE0', activebackground = '#000000')
R2.place(x=350,y=275)
#=========================== RADIO BUTTON SELECTION ============================
cancel_button = Button(root, text='Cancel', width = 12, bg=('#000000'), fg = ('#B3CDE0'), activebackground = '#000000', command = close)
cancel_button.place(x=590,y=270)
recover_button = Button(root, text='Open', width = 12, bg=('#000000'), fg = ('#B3CDE0'), activebackground = '#000000',command = getpass)
recover_button.place(x=480,y=270)
root.mainloop()
When you want to make a widget disappear and then appear again, you don't actually have to define it and all it's options again.
#=========================== CHECK BUTTON SELECTION ============================
pass_entry = Entry(root, width = 20, textvariable = pass_1, bg='#000000', fg='#B3CDE0')
selection = Label(root, text='Enter OS Password:', bg='#000000', fg='#B3CDE0')
def select_1():
if var1.get()==1:
pass_entry.place(x=250,y=275)
pass_entry.focus_set()
selection.place(x=140,y=275)
elif var1.get()==0:
pass_entry.place_forget()
selection.place_forget()
else:
return
Notice how pass_entry and selection are defined outside of the function and only placed inside the function.
What's a bit confusing though is why you're using two separate buttons for "yes" and "no" if checking them doesn't automatically uncheck the other one (i assume that's the purpose of the two buttons).
If that's what you want to do, you should use the Radiobutton widget instead. Notice how both of them affect the same variable and each of them have one value.
#=============================== RADIO BUTTONS =================================
R1 = Radiobutton(root, text="Yes", variable = var1, value = 1, height=1, width = 15, activebackground = '#000000', bg='#000000', fg='#B3CDE0', command=select_1)
R1.place(x=350,y=250)
R2 = Radiobutton(root, text="No", variable = var1, value = 0, height=1, width = 15, activebackground = '#000000', bg='#000000', fg='#B3CDE0', command=select_1)
R2.place(x=350,y=275)
I also tidied up the other options a bit too, you had some random spacing in there.

How to get text from Entry widget

I've looked at several posts on stackOverflow that explain the answer but no matter which I use, I never can get the string from my entry widget; it just detects a string of ""
here's my code:
def buttonTest():
global score
gui.title("Test")
for child in gui.winfo_children():
child.destroy()
global questionText
global questionAnswer
questionText = StringVar()
questionAnswer = 0
question = Label(gui, textvariable = questionText, fg = "black", bg = "white")
question.grid(row = 0, column = 1)
userInput = StringVar()
input = Entry(gui, textvariable = userInput)
input.grid(row = 1, column = 0)
swapQuestion()
checkAns = Button(text = "Check answer", command = partial(checkAnswer, userInput.get(), questionAnswer), fg = "black", width=10)
checkAns.grid(row = 1, column = 2)
Please read and follow this SO help page. Your code is missing lines needed to run and has lines that are extraneous to your question. It is also missing indentation.
Your problem is that you call userInput.get() just once, while creating the button, before the user can enter anything. At that time, its value is indeed ''. You must call it within the button command function, which is called each time the button is pressed.
Here is a minimal complete example that both runs and works.
import tkinter as tk
root = tk.Tk()
user_input = tk.StringVar(root)
answer = 3
def verify():
print(int(user_input.get()) == answer) # calling get() here!
entry = tk.Entry(root, textvariable=user_input)
entry.pack()
check = tk.Button(root, text='check 3', command=verify)
check.pack()
root.mainloop()
Simple example:
from tkinter import *
# Get Entry contents
def print_input():
print(input_variable.get())
window = Tk()
# Create widgets
input_variable = StringVar()
entry_variable = Entry(window, textvariable=input_variable).grid(row=0, column=0)
button_submit = Button(window, text="Submit",command=print_input).grid(row=1, column=0)
window.mainloop()
Where:
input_variable is your variable
entry_variable is the entry box
button_submit calls print_input() to fetch and print the entry_variable's contents which is stored in input_variable

Resources