I have a little problem with the checkbtn value.
Here is my code:
def select_data():
select_window = Tk()
var_autotrader = IntVar()
check_sites_autotrader = Checkbutton(select_window, text = 'autotrader', variable = var_autotrader)
var_bestcarfinder = IntVar()
var_cardady = IntVar()
var_cars = IntVar()
var_car_gurus = IntVar()
var_iseecars = IntVar()
var_truecar = IntVar()
check_sites_bestcarfinder = Checkbutton(select_window, text = 'bestcarfinder', variable = var_bestcarfinder)
check_sites_cardaddy = Checkbutton(select_window, text = 'cardady', variable = var_cardady)
check_sites_cars = Checkbutton(select_window, text = 'cars.com', variable = var_cars)
check_sites_car_gurus = Checkbutton(select_window, text = 'car gurus', variable = var_car_gurus)
check_sites_iseecars = Checkbutton(select_window, text = 'iseecars', variable = var_iseecars)
check_sites_truecar = Checkbutton(select_window, text = 'truecar', variable = var_truecar)
check_sites_autotrader.grid(row = 0, column = 0, sticky = W)
check_sites_bestcarfinder.grid(row = 1, column = 0, sticky = W)
check_sites_cardaddy.grid(row = 2, column = 0, sticky = W)
check_sites_cars.grid(row = 3, column = 0, sticky = W)
check_sites_car_gurus.grid(row = 4, column = 0, sticky = W)
check_sites_iseecars.grid(row = 5, column = 0, sticky = W)
check_sites_truecar.grid(row = 6, column = 0, sticky = W)
def upload_selected_data():
print(var_autotrader.get())// This one does not give me 1 when check is on
btn = Button(select_window, text = 'go!', command = upload_selected_data)
btn.grid(row = 7, column = 0, sticky = W+E)
The trouble is that my variable is not changing when I check the box
Thank you very much!
Probably you created your "main window" with Tk(). In this case you should create another window with the Toplevel() (Not with Tk()).
It means in your case you should change the select_window = Tk() line to select_window = Toplevel()
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Images")
root.iconbitmap(r"C:\Users\DellABD\Downloads\lolol.ico")
my_img1 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\illusion.jpg"))
my_img2 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0053.jpg"))
my_img3 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0054.jpg"))
my_img4 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0055.jpg"))
my_img5 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0056.jpg"))
my_img_list = [my_img1, my_img2, my_img3, my_img4, my_img5]
my_label = Label(image = my_img1)
my_label.grid(row = 0, column = 0, columnspan = 3)
def forward(image_number):
global my_label
global back
global forward
my_label.grid_forget()
my_label = Label(my_img_list[image_number - 1])
my_label.grid(row = 0, column = 0, columnspan = 3)
def back():
global my_label
global back
global forward
global my_label
global back
global forward
my_label.grid_forget()
my_label = Label(my_img_list[image_number - 1])
button_forward = Button(root, text = ">>>", command = lambda: image_number + 1)
button_back = Button(root, text = "<<<", command = lambda: image_number - 1)
if button_back == 1:
button_back = Buuton(rrot, text = "<<<", state = DISABLED)
button_back.grid(row = 1, column = 0)
button_forward.grid(row = 1, column = 2)
my_label.grid(row = 0, column = 0, columnspan = 3)
button_back = Button(root, text = "<<<", padx = 20, pady = 20, command = back)
button_forward = Button(root, text = ">>>", padx = 20, pady = 20, command = lambda: forward(2))
button_quit = Button(root, text = "Exit", padx = 20, pady = 20, command = root.quit)
button_back.grid(row = 1, column = 0)
button_forward.grid(row = 1, column = 2)
button_quit.grid(row = 1, column = 1)
root.mainloop()
when I run the programme and press the Forward button it shows me an error. I wanted to move on next image by pressing forward and when I press the first image disappears but next image dont show up and shows error.
if master._last_child_ids is None:
AttributeError: 'PhotoImage' object has no attribute '_last_child_ids'
The problem is this code:
Label(my_img_list[image_number - 1])
You are trying to use an image as the parent/master of a label. You need to use this instead:
Label(image=my_img_list[image_number - 1])
Im making my own cypher encryption and I want to put the result into the entry field called output. Now im just using print() so I could test if I got a result. But that was only for testing. This is one of the first times I used Python so if there are some other things I could have done better please let me know :)
this is what I have so far.
from tkinter import *
#Make frame
root = Tk()
root.geometry("500x300")
root.title("Encryption Tool")
top_frame = Frame(root)
bottom_frame = Frame(root)
top_frame.pack()
bottom_frame.pack()
#Text
headline = Label(top_frame, text="Encryption Tool", fg='black')
headline.config(font=('Courier', 27))
headline.grid(padx=10, pady=10)
Key = Label(bottom_frame, text="Key:", fg='black')
Key.config(font=('Courier', 20))
Key.grid(row=1)
Text_entry = Label(bottom_frame, text="Text:", fg='black')
Text_entry.config(font=('Courier', 20))
Text_entry.grid(row=2)
Output_text = Label(bottom_frame, text="Output:", fg='black')
Output_text.config(font=('Courier', 20))
Output_text.grid(row=3)
Key_entry = Entry(bottom_frame)
Key_entry.grid(row=1, column=1)
Text_entry = Entry(bottom_frame)
Text_entry.grid(row=2, column=1)
Output_text = Entry(bottom_frame)
Output_text.grid(row=3, column=1)
#Encryption_button
def encrypt():
result = ''
text = ''
key = Key_entry.get()
text = Text_entry.get()
formule = int(key)
for i in range(0, len(text)):
result = result + chr(ord(text[i]) + formule + i * i)
result = ''
Encryption_button = Button(bottom_frame, text="Encrypt", fg='black')
Encryption_button.config(height = 2, width = 15)
Encryption_button.grid(row = 4, column = 0, sticky = S)
Encryption_button['command'] = encrypt
#Decryption_button
def decrypt():
result = ''
text = ''
key = Key_entry.get()
text = Text_entry.get()
formule = int(key)
for i in range(0, len(text)):
result = result + chr(ord(text[i]) - formule - i * i)
print(result)
result = ''
Decryption_button = Button(bottom_frame, text="Decrypt", fg="black")
Decryption_button.config(height = 2, width = 15)
Decryption_button.grid(row = 5, column = 0, sticky = S)
Decryption_button['command'] = decrypt
#Quit_button
def end():
exit()
Quit_button = Button(bottom_frame, text="Quit", fg='black')
Quit_button.config(height = 2, width = 15)
Quit_button.grid(row = 6, column = 0, sticky = S)
Quit_button['command'] = end
root.mainloop()
The most common way to do this with tkinter would be with a StringVar() object you can connect to the Entry object (Some documentation here).
output_entry_value = StringVar()
Output_text = Entry(bottom_frame, textvariable=output_entry_value)
Output_text.grid(row=3, column=1)
then you can .set() the result in the stringvar, and it will update in the entries you connected it to:
output_entry_value.set(result)
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 3 years ago.
I am trying to write a program using a tkinter GUI to calculate a few things. My get() function isn't working and I'm not sure why
from tkinter import *
from math import *
root = Tk()
v = 0
l = 0
w = 0
t1 = 0
t0 = 0
label1 = Label(root, text = "Enter value for length (mm) :").grid(columnspan = 2, sticky = "E")
length = Entry(root).grid(row = 0, column = 2)
label2 = Label(root, text = "Enter value for volume (mm^2) :").grid(columnspan = 2, sticky = "E")
volume = Entry(root).grid(row = 1, column = 2)
label3 = Label(root, text = "Enter value for the thickness of the cylinder (mm) :").grid(columnspan = 2, sticky = "E")
thickness = Entry(root).grid(row = 2, column = 2)
label4 = Label(root, text = "Enter value for starting temperature (K) :").grid(columnspan = 2, sticky = "E")
st_T = Entry(root).grid(row = 3, column = 2)
label5 = Label(root, text = "Enter value for finishing temperature (K) :").grid(columnspan = 2, sticky = "E")
end_T = Entry(root).grid(row = 4, column = 2)
def save():
v = volume.get()
l = length.get()
w = thickness.get()
t0 = st_T.get()
t1 = end_T.get()
global values
values = [v, l, w, t1, t0]
answer = StringVar()
labelans = Label(root, textvariable = answer).grid(columnspan = 3,)
answer.set("Answer = ")
def area_circle():
global answer
answer = v / l
print(answer)
Button(root, text = "Submit", command = save()).grid(row = 6, column = 0)
root.mainloop()
Obviously there are variables i'm not using yet but im trying to get the first part right first.
For v the error displayed is:
Message='NoneType' object has no attribute 'get'
The Grid geometry manager puts the widgets in a 2-dimensional table.
The master widget is split into a number of rows and columns, and each
“cell” in the resulting table can hold a widget.
What is important Grid() is returning NoneValue
if you are doing like:
length = Entry(root).grid(row = 0, column = 2)
in your variable length you will have NoneValue
you should do it like :
length = Entry(root)
length.grid(row = 0, column = 2)
your code:
label1 = Label(root, text = "Enter value for length (mm) :")
label1.grid(columnspan = 2, sticky = "E")
length = Entry(root)
length.grid(row = 0, column = 2)
label2 = Label(root, text = "Enter value for volume (mm^2) :")
label2.grid(columnspan = 2, sticky = "E")
volume = Entry(root)
volume.grid(row = 1, column = 2)
label3 = Label(root, text = "Enter value for the thickness of the cylinder (mm) :")
label3.grid(columnspan = 2, sticky = "E")
thickness = Entry(root)
thickness.grid(row = 2, column = 2)
label4 = Label(root, text = "Enter value for starting temperature (K) :")
label4.grid(columnspan = 2, sticky = "E")
st_T = Entry(root)
st_T.grid(row = 3, column = 2)
label5 = Label(root, text = "Enter value for finishing temperature (K) :")
label5.grid(columnspan = 2, sticky = "E")
end_T = Entry(root)
end_T.grid(row = 4, column = 2)
def save():
v = volume.get()
l = length.get()
w = thickness.get()
t0 = st_T.get()
t1 = end_T.get()
global values
values = [v, l, w, t1, t0]
answer = StringVar()
labelans = Label(root, textvariable = answer).grid(columnspan = 3,)
answer.set("Answer = ")
def area_circle():
global answer
answer = v / l
print(answer)
Button(root, text = "Submit", command = save()).grid(row = 6, column = 0)
root.mainloop()
output:
The command argument should be a callback, not the actual call:
Button(root, text="Submit", command=save).grid(row=6, column=0)
The below program was working fine few minutes back. I made a change,ran the code, made a small mistake, Spyder crashed and now it either cannot find Frame or Groove or something else. At the moment it says GROOVE not defined.
Tried writing it in lower case and with quotes. When I do with quotes it says: TclError: bad relief "Groove": must be flat, groove, raised, ridge, solid, or sunken. When I do lower or upper case without quotes says groove not defined.
from RiskFactorConversion import *
from tkinter import ttk, StringVar, Frame, Label, Tk, Entry
mainwindow = Tk()
mainwindow.title("Risk Factor Conversion")
datatype = StringVar()
dataconvention = StringVar()
mdlname = StringVar()
instancevalue = StringVar()
axisvalue = StringVar()
def g():
datatype = e1.get()
dataconvention = e2.get()
mdlname = e3.get()
instancevalue = e4.get()
r1 = rates.srtqualifier(mdlname,datatype,dataconvention,instancevalue)
l5["text"] =r1.makequalifier()
def f():
datatype = e5.get()
dataconvention = e6.get()
axisvalue = e8.get()
fx1 = fx.felixfxvol(datatype,dataconvention,axisvalue)
l11["text"] =fx1.fxvol()
def h():
datatype = en1.get()
dataconvention = en2.get()
fx2 = fx.murexfx(datatype,dataconvention)
la4["text"] =fx2.makequalifier()
#########Felix Frame####################################
frame1 = Frame(bg="white", colormap="new", padx = 10, relief=GROOVE, borderwidth=2)
frame1.grid(row = 0, column = 0)
l0 = Label(frame1, text= "FELIX Rates", pady =5, font = ('Georgia',14,'bold'), bg="white")
l0.grid(row = 0, column = 0,sticky= W )
l1 = Label(frame1, text= "Please provide Data Type:",bg="white", justify = "right",pady =5 )
l1.grid(row = 1, column = 0, sticky= E )
e1 = Entry(frame1,bd = 2, width =50, textvariable = datatype )
e1.grid(row = 1, column = 1)
e1.focus_set()
l2 = Label(frame1,bg="white", text= "Please provide Data Convention:",justify = "right", pady = 5)
l2.grid(row = 2, column = 0,sticky= E)
e2 = Entry(frame1,bd = 2, width =50, textvariable = dataconvention )
e2.grid(row = 2, column = 1)
l3 = Label(frame1,bg="white", text= "Please provide Model Type:", justify = "right",pady = 5)
l3.grid(row = 3, column = 0,sticky= E)
e3 = ttk.Combobox(frame1,width =45, textvariable = mdlname, state='readonly')
e3['values'] = ('IR_SABR','FX_LN','IR_LGM','IR_LMM','IR_SABR_FX_LN_GC','IR_SABR_GC','INFLATION_SABR')
e3.grid(row = 3, column = 1)
l4 = Label(frame1,bg="white", text= "Please provide Instance Name:", justify = "right",pady = 5)
l4.grid(row = 4, column = 0,sticky= E)
e4 = Entry(frame1,bd = 2, width =50, textvariable = instancevalue )
e4.grid(row = 4, column = 1)
bfelix = Button(frame1, text = "Press to get Qualifier Value", pady = 10, command = g)
bfelix.grid(row = 5, column = 0)
l5 = Label(frame1,bg="white", text= "" , justify = "right",pady = 5)
l5.grid(row = 5, column = 1)
################################################################
############FELIX FX############################################
frame2 = Frame(bg="white", colormap="new", padx = 10, relief=GROOVE, borderwidth=2)
frame2.grid(row = 0, column = 1)
l6 = Label(frame2, text= "FELIX FX", pady =5, font = ('Georgia',14,'bold'), bg="white")
l6.grid(row = 0, column = 0,sticky= W )
l7 = Label(frame2, text= "Please provide Data Type:",bg="white", justify = "right",pady =5 )
l7.grid(row = 1, column = 0, sticky= E )
e5 = Entry(frame2,bd = 2, width =50, textvariable = datatype)
e5.grid(row = 1, column = 1)
e5.focus_set()
l8 = Label(frame2,bg="white", text= "Please provide Data Convention:",justify = "right", pady = 5)
l8.grid(row = 2, column = 0,sticky= E)
e6 = Entry(frame2,bd = 2, width =50, textvariable = dataconvention )
e6.grid(row = 2, column = 1)
l10 = Label(frame2,bg="white", text= "Please provide axis 2 value:", justify = "right",pady = 5)
l10.grid(row = 3, column = 0,sticky= E)
e8 = Entry(frame2,bd = 2, width =50, textvariable = axisvalue )
e8.grid(row = 3, column = 1)
bfelixfx = Button(frame2, text = "Press to get Qualifier Value", pady = 10, command = f)
bfelixfx.grid(row = 4, column = 0)
l11 = Label(frame2,bg="white", text= "" , justify = "right",pady = 5)
l11.grid(row = 4, column = 1)
#####################################################################
############MUREX FX############################################
frame3 = Frame(bg="white", colormap="new", padx = 10, relief=GROOVE, borderwidth=2)
frame3.grid(row = 1, column = 1)
la = Label(frame3, text= "Murex FX", pady =5, font = ('Georgia',14,'bold'), bg="white")
la.grid(row = 0, column = 0,sticky= W )
la1 = Label(frame3, text= "Please provide Risk Factor Type:",bg="white", justify = "right",pady =5 )
la1.grid(row = 1, column = 0, sticky= E )
en1 = ttk.Combobox(frame3, width =45, textvariable = mdlname, state='readonly')
en1['values'] =('FX ATM VOLATILITY','FX BUTTERFLY 10D','FX BUTTERFLY 25D','FX RISK REVERSAL 10D','FX RISK REVERSAL 25D')
en1.grid(row = 1, column = 1)
en1.focus_set()
la2 = Label(frame3,bg="white", text= "Please provide Currency Pair:",justify = "right", pady = 5)
la2.grid(row = 2, column = 0,sticky= E)
en2 = Entry(frame3,bd = 2, width =50, textvariable = dataconvention )
en2.grid(row = 2, column = 1)
bmurexfx = Button(frame3, text = "Press to get Qualifier Value", pady = 10, command = h)
bmurexfx.grid(row = 4, column = 0)
la4 = Label(frame3,bg="white", text= "" , justify = "right",pady = 5)
la4.grid(row = 4, column = 1)
#####################################################################
mainwindow.mainloop()
Groove is a constant defined in Tkinter and since you are only importing specfic functions from Tkinter that doesn't include Groove, you need to add GROOVE to it or add
import tkinter as tk
and then set relief=tk.GROOVE