Hey I would like to create a frame which contains a Listbox and some buttons. What I would like to do is to somehow pass as an argument a name of this listbox. This is my current code in which I set the name of this listbox to master.thisListbox but I would like to pass it somehow as argument. Is it possible or should I separately crate a listbox in my App and the pass it as an argument to a frame? I'm going to create a lot of such listboxes in my App hence the name of listbox shouldn't be the same. I'm pretty new in OOP.
class myApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.dateFrame = DateFrame(self)
self.dateFrame.grid(row = 0, column = 0)
print(self.day.get())
self.myFrame = ListboxFrame(self, "Label!", [1,2,3])
self.myFrame.grid(row = 1, column = 0)
x = self.thisListbox.get(0, END)
print(x)
class DateFrame(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
# Day
ttk.Label(self, text = "Day", width = 4).grid(row = 0, column = 0, padx = 3, pady = 3)
master.day = IntVar(master, value = 31)
self.dayCB = ttk.Combobox(self, values = [x for x in range(1, 32)], textvariable = master.day, width = 4)
self.dayCB.grid(row = 1, column = 0, padx = 3, pady = 3)
# Month
ttk.Label(self, text = "Month", width = 6).grid(row = 0, column = 1, padx = 3, pady = 3)
self.month = IntVar(master, value = 12)
self.monthCB = ttk.Combobox(self, values = [x for x in range(1, 13)], textvariable = self.month, width = 4)
self.monthCB.grid(row = 1, column = 1, padx = 3, pady = 3)
# Year
ttk.Label(self, text = "Year", width = 4).grid(row = 0, column = 2, padx = 3, pady = 3)
self.year = IntVar(master, value = 2021)
self.yearCB = ttk.Spinbox(self, from_ = 2020, to = 2100, textvariable = self.year, width = 6)
self.yearCB.grid(row = 1, column = 2, padx = 3, pady = 3)
class ListboxFrame(ttk.Frame):
def __init__(self, master, labelText, values, selectmode = "extended", height = 6, width = 30):
ttk.Frame.__init__(self, master)
# Listbox
ttk.Label(self, text = labelText).grid(row = 0, column = 0, columnspan = 3)
master.thisListbox = tk.Listbox(self, selectmode = selectmode, height = height, width = width)
master.thisListbox.grid(row = 1, column = 0, columnspan = 3, padx = 2, pady = 2)
# Entry
self.entry = ttk.Entry(self)
self.entry.grid(row = 2, column = 0, padx = 2, pady = 2)
# Buttons
self.addButton = ttk.Button(self, text = "Add", width = 4, command = self.Add)
self.addButton.grid(row = 2, column = 1, padx = 2, pady = 2)
self.deleteButton = ttk.Button(self, text = "Delete", width = 6, command = self.Delete)
self.deleteButton.grid(row = 2, column = 2, padx = 2, pady = 2)
for v in values:
master.thisListbox.insert(END, v)
def Add(self):
if self.entry.get() == "": return
master.thisListbox.insert(END, self.entry.get())
self.entry.delete(0, END)
def Delete(self):
for index in reversed(master.thisListbox.curselection()):
master.thisListbox.delete(index)
# listbox.config(height = listbox.size())
I have a problem regarding tkinter's grid_forget() method. I have 2 pages in a notebook and I want to show certain widgets on the second page based off the user's selected options in the first page, I have a multiple choice menu and the grid_forget() method seems to work well until I select 2 or more options from the menu. I tried creating the widgets when an option is selected and place them based on the choice, no luck there, also tried creating them with the rest of the widgets and when the user selected an option I would simply just use grid to place them on the screen, also no luck there. I created a demo below, sorry for potential mistakes.
import tkinter as tk
from tkinter import ttk
SMALL_FONT = ("calibri", 16)
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.notebook = ttk.Notebook(self, height = "900", width = "1600")
self.notebook.grid(row = 0, column = 0)
self.frame_pasul3 = tk.Frame(self.notebook)
self.frame_pasul1 = tk.Frame(self.notebook)
self.notebook.add(self.frame_pasul1, text = "First page")
self.notebook.add(self.frame_pasul3, text = "Second page")
self.first_page()
def first_page(self):
options = ["Urmarire mobiliara",
"Urmarire imobiliara",
"Predarea silita bunuri imobile",
"Predarea silita bunuri mobile",
"Obligatia de a face",
"Executare minori"]
menubutton_modalitate_exec = tk.Menubutton(self.frame_pasul1, text="Alegeti o modalitate de executare",
indicatoron=True, borderwidth=1, fg = "#000000",relief="raised")
menu_modalitate_exec = tk.Menu(menubutton_modalitate_exec, tearoff=False)
menubutton_modalitate_exec.configure(menu=menu_modalitate_exec)
menubutton_modalitate_exec.grid(row = 4, column = 1)
self.modalitate = {}
for choice in options:
self.modalitate[choice] = tk.StringVar()
menu_modalitate_exec.add_checkbutton(label=choice, variable=self.modalitate[choice],
onvalue=1, offvalue=0,
command=self.printValues)
self.second_page()
def second_page(self):
self.frame3_titlu_exec = ttk.Frame(self.frame_pasul3)
self.frame3_titlu_exec.grid()
self.frame3_text = ttk.Frame(self.frame_pasul3)
self.frame3_text.grid()
self.frame3_creante = tk.Frame(self.frame_pasul3)
self.frame3_creante.grid()
self.frame3_reprezentand_obligatia = tk.Frame(self.frame_pasul3)
self.frame3_judecatorie = ttk.Frame(self.frame_pasul3)
self.frame3_judecatorie.grid()
self.frame3_texte = tk.Frame(self.frame_pasul3)
self.frame3_texte.grid()
ttk.Label(self.frame3_titlu_exec, font = SMALL_FONT, text = "Titlu Executoriu").grid(row = 0, column = 0, columnspan = 4 ,pady = 10)
ttk.Button(self.frame3_titlu_exec, text = "Contract de credit.").grid(row = 1, column = 0, padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Sentinta civila.").grid(row = 1, column = 1, padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Contract notarial.").grid(row = 1, column = 2,padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Act de adjudecare.").grid(row = 1, column = 3, padx = 10, ipadx = 15, ipady = 5)
self.entry = tk.Text(self.frame3_text, height = 2, wrap = "word", font = ("Helvetica", 10))
self.entry.grid(row = 0, column = 1, padx = 10, pady = 15)
ttk.Button(self.frame3_text, text = "Incarca titlu").grid(row = 0, column = 2, padx = 10, pady = 15)
ttk.Label(self.frame3_creante, font = SMALL_FONT, text = "Creante").grid(row = 0, column = 0)
self.btn_adauga_creante = ttk.Button(self.frame3_creante, text = "Adauga")
self.btn_adauga_creante.grid(row = 0, column = 3)
self.reprezentand_fapt_label = ttk.Label(self.frame3_judecatorie, font = SMALL_FONT, text = "Ce reprezinta fapta.")
self.reprezentand_fapt_label.grid(row = 2, column = 1)
self.reprezentand_creante = tk.Text(self.frame3_judecatorie, height = 3, width = 70)
self.reprezentand_creante.grid(row = 3 , column = 1, pady = 15)
ttk.Label(self.frame3_texte, font = SMALL_FONT, text = "Judecatorie").grid(row = 4, column = 1)
options_jud = ["optiunea 2.",
"test 3",
"test 4",
"test 5"]
self.judecatorie = ttk.Combobox(self.frame3_texte, values = options_jud)
self.judecatorie.set("Selecteaza o judecatorie.")
self.judecatorie.grid(row = 5, column = 1)
ttk.Button(self.frame3_texte, text = "Pasul 2. Parti dosar.").grid(row = 6, column = 0, padx = 15, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_texte, text = "Pasul 4. Cheltuieli de executare").grid(row = 6, column = 3, ipadx = 15, ipady = 5)
def printValues(self):
for name, var in self.modalitate.items():
if var.get() == "1" and (name == "Predarea silita bunuri imobile" or name == "Predarea silita bunuri mobile" or name == "Obligatia de a face" or name == "Executare minori"):
self.reprezentand_creante_label = tk.Label(self.frame3_judecatorie, font = SMALL_FONT, text = "Ce reprezinta creanta.")
self.reprezentand_creante = tk.Text(self.frame3_judecatorie, wrap = "word", height = 3, width = 70)
self.ok_modalitate_exec = 1
self.reprezentand_creante_label.grid(row = 0, column = 1, padx = 15, pady = 15)
self.reprezentand_creante.grid(row = 1, column = 1, padx = 15, pady = 15)
print("Avem 1 la cele 4", name, var.get())
break
elif var.get() == "0" and (name == "Predarea silita bunuri imobile" or name == "Predarea silita bunuri mobile" or name == "Obligatia de a face" or name == "Executare minori"):
print("Avem 0 la cele 4", name, var.get())
self.ok_modalitate_exec = 0
self.reprezentand_creante_label.grid_forget()
self.reprezentand_creante.grid_forget()
break
if __name__ == "__main__":
main_window = tk.Tk()
app = App(main_window)
app.grid()
main_window.mainloop()
i want it to expand the size accordingly if the window is expanded, so far i use the grid.row/column configure to make its weight =1 but it will be all messed up it i expand the window.
import time
import tkinter as tk
#Initialise the window
clock = tk.Tk()
clock.title('Easy CLock')
clock.configure(bg='#121212')
clock.columnconfigure(0, weight = 1)
clock.rowconfigure(0, weight = 1)
border_effects = {
"flat": tk.FLAT,
"sunken": tk.SUNKEN,
"raised": tk.RAISED,
"groove": tk.GROOVE,
"ridge": tk.RIDGE,
}
#Logo will be under the main parent
logo = tk.PhotoImage(file = r'C:\Users\User\VSC\Alarm\Logo1.png')
logo_size = logo.subsample(5)
#Time and Date function
def time_date():
# current time
current_time = time.strftime('%H:%M:%S')
current_date = time.strftime(r'%m/%d/%Y')
clock.after(200, time_date)
#Displays the time
c_time = tk.Label(f_time, text = current_time, fg='white', bg='#121212', font=('Verdana', 30))
c_date = tk.Label(f_time, text = current_date, font=('Verdana', 10), fg='white', bg='#121212')
c_time.grid(column=0, row=0)
c_date.grid(column=0, row=1)
#alarm button command
def alarm_func():
#Alarm label
c_clicked = tk.Label(f_alarm, text='Alarm Interface', fg='white', bg='#121212')
c_clicked.grid(column=0, row=1, sticky = 'N')
def recall_frame(event):
if event == f_alarm:
event.grid_forget()
f_time.grid(column=0, row =1, columnspan = 4, sticky = 'N')
elif event == f_time:
event.grid_forget()
f_alarm.grid(column=0, row=1, columnspan = 4, rowspan = 2)
def back_func():
pass
#Creating Frames
f_time = tk.Frame(clock) #Clock Button
f_alarm = tk.Frame(clock) #Alarm Buttton
#configure the frames
f_time.configure(bg = '#121212')
f_alarm.configure(bg = '#121212')
#Setting label in the frame
f_lbl = tk.Label(clock, text= ' Simplistic Clock', image = logo_size, font=('Verdana', 30), fg='white', bg='#121212', compound = tk.LEFT)
time_but = tk.Button(clock, text='Clock', command= lambda :[time_date(), recall_frame(f_alarm)], bg='#f39c12', width = 15, relief = border_effects['ridge'])
alarm_but = tk.Button(clock, text = 'Alarm', command = lambda :[alarm_func(), recall_frame(f_time)], bg='#f39c12', width = 15, relief = border_effects['ridge'])
quit_but = tk.Button(clock, text='Exit', command = clock.quit, bg='#f39c12', width = 15, relief = border_effects['ridge'])
back_but = tk.Button(clock, text = 'Back To Home', command = back_func, bg='#f39c12', width = 15, relief = border_effects['ridge'])
f_lbl.config(borderwidth = 4, relief = border_effects['sunken'])
f_lbl.grid_columnconfigure(0, weight = 1)
#Putting it on the frames
f_lbl.grid(column = 0, row = 0, columnspan = 4, sticky = 'N')
time_but.grid(column = 0, row = 3)
alarm_but.grid(column = 1, row = 3)
back_but.grid(column = 2, row = 3)
quit_but.grid(column = 3, row = 3)
clock.mainloop()
also, why does the border in the f_lbl, simplistic clock not fully extended through out all 4 column since i put columnspan = 4 , and weight = 1 , should't it expand fully through all 4 columns?
I have three list boxes and the first one has a certain number of options. When the user selects an option, the second list box needs to fill depending on previous selection. Here is the code I tried. I am using Python 2.7
updated:
import sys
import Tkinter as TK
font = ('Times New Rome', 12, 'Bold')
Template = ''
Entrybox1 = ''
Entrybox2 = ''
Lb1 = ''
Lb2 = ''
Lb3 = ''
class smartstopkiosk_tk(TK.Tk):
def __init__(self, parent):
TK.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
#self.parent.title("Smartstop Kiosk")
global Entrybox1
global Entrybox2
global LocalTemplate
global Software
seltext = ""
software = ('Microsoft', 'Abode', 'SAP', 'Lotus Notes', 'Jive','Cisco', 'Chrome')
hardware = ("Computer", "Charger", " USB Headset", "Keyboard", "Mouse", "Webcam")
microsoft = ("Outlook", "Lync", "Word", "Excel", "PowerPoint", "Visio", "Project", "Publisher")
adobe = ("Adobe Reader", "Java", "Flash", "Adobe Pro")
cisco = ('IP Communicator', '')
wireless = ("MYLOW", "Corporate", "Visitor", "MYLTW")
printers = ("2N", "2S", "3N", "3S", "4N", "4S", "5N", "5S")
mobile = ("Blackberry", "iPhone","iPad")
AD = ("Unlock", "Reset")
LocalTemplate = ('User Assistance with Software.', 'User Assistance with Hardware.', 'Mobile Device Support.',
'Software Installation Request', 'Hardware Request', 'Wireless Connection', 'Password Reset/Username Unlocked',
'Add Printer')
#creates font types
font = ('Times New Roman', 14, 'bold')
font2 = ('Times New Roman',12, 'bold')
#Creates Instructions for Users
stepOne = TK.LabelFrame(self, text=" 1. User Information: ", font = font2)
stepOne.grid(row=0, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
stepTwo = TK.LabelFrame(self, text="2. Select Program or Hardware Problems", font = font2)
stepTwo.grid(row=3, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
stepThree = TK.LabelFrame(self, text="Please Enter the Problem or Your Request:", font = font2)
stepThree.grid(row=6, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
#Creates Label for Users
Label1 = TK.Label(stepOne, text = "First Name", font = font)
Label1.grid(column = 2, row = 0, sticky = 'w', padx = (10, 10))
Label2 = TK.Label(stepOne, text = "Last Name", font = font)
Label2.grid(column = 4, row = 0, sticky = 'w', padx = (10, 10))
Label3 = TK.Label(stepOne, text = "UserName", font = font)
Label3.grid(column = 6, row = 0, sticky = 'w', padx = (10, 10))
#Creates Entry Box for User input
self.entry = TK.Entry(stepOne, width = 30)
self.entry.grid(column = 2, row = 1,padx = (10, 10))
entry2 = TK.Entry(stepOne, width = 30)
entry2.grid(column = 4, row = 1,padx = (10, 10))
entry3 = TK.Entry(stepOne, width = 30)
entry3.grid(column = 6, row = 1, columnspan = 2, padx = (10, 10))
#Creates User Input Box
Usertext = TK.Text(stepThree, height = 10, width = 110, font = font)
Usertext.grid(column = 2, row = 15, padx = (10, 10))
#Creates List Boxes
Listbox1 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
Listbox1.grid(column = 2, row = 1, padx = (10, 10))
for i in LocalTemplate:
Listbox1.insert(TK.END, i)
Listbox1.bind("<<ListboxSelect>>", self.selection)
Listbox2 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
Listbox2.grid(column = 4, row = 1, padx = (10, 10))
for x in seltext:
Listbox2.insert(TK.END, x)
Listbox2.bind("<<ListboxSelect>>", self.selection)
Listbox3 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
Listbox3.grid(column = 6, row = 1, padx = (10, 10))
#Creates Buttons
Submit = TK.Button(self, text = 'Submit', font = font2)
Submit.grid(column = 3, row = 10, sticky = 'we', padx = (10, 10))
Startover = TK.Button(self, text = 'Cancel', font = font2)
Startover.grid(column = 4, row = 10, sticky = 'we', padx = (10, 10))
def selection(self, val):
sender = val.Listbox1
index = listbox1.curselection()
value = Listbox1.get(index[0])
if index == 1 or index == 4:
seltext = software
elif index == 2 or index == 5:
seltext = hardware
elif index == 3:
seltext = mobile
elif index == 6:
seltext = wireless
elif index == 7:
seltext = AD
elif index == 8:
seltext = printer
def main():
app = smartstopkiosk_tk(None)
app.geometry("1300x768")
app.mainloop()
if __name__ == "__main__":
main()
I keep getting this error Traceback
File "C:\python27\lib\lib-tk\Tkinter.py", line 1845, in getattr
return getattr(self.tk, attr)
AttributeError: selection
UPDATED!
Here is the how I was able to have text in the first list box and have the second list box fill depending on your choice.
def getchoice(event):
seltext = ''
INDEX = Listbox1.curselection()
if INDEX == (0,) or INDEX == (4,):
seltext = software
elif INDEX == (1,) or INDEX == (5,):
seltext = hardware
elif INDEX == (2,):
seltext = mobile
elif INDEX == (3,):
seltext = wireless
elif INDEX == (6,):
seltext = AD
elif INDEX == (7,):
seltext = printers
print INDEX
Listbox2.delete(0, TK.END)
for x in seltext:
Listbox2.insert(TK.END, x)
#Creates List Boxes
Listbox1 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
Listbox1.grid(column = 2, row = 1, padx = (10, 10))
for x in LocalTemplate:
Listbox1.insert(TK.END, x)
Listbox1.bind("<<ListboxSelect>>", getchoice)
Listbox2 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
Listbox2.grid(column = 4, row = 1, padx = (10, 10))
Listbox2.bind("<<ListboxSelect>>", getchoice2)
Listbox3 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
Listbox3.grid(column = 6, row = 1, padx = (10, 10))
Alright #Jobes23, This was very fun. Now there were a few 'noobish mistakes' in your code: at the start you had defined lbl1, 2, 3 but instead used label1, 2, 3, making lbl1 useless, there were a few unnecessary variables, and added code, also when setting the variables for the listboxes you sometimes used '', and others "" around the words, this doesn't affect the code but it is good to have the same convention throughout your code, it did appear (like Bryan Oakley said) that you had just combined bits and pieces of code from everywhere without regard to how they work alone and together - it is crucial to understand the code to be able to solve problems in it. I have tried to get rid of unnecessary code, but am unsure how much of it you need.
Now I said earlier that self.selection wasn't an option, instead I got rid of the listbox.bind(...) and made up a button with a command which led to a function which took the selected option and used the result to set the second listbox to a string, which is what you wanted to achieve I think.
import Tkinter as TK
class smartstopkiosk_tk(TK.Tk):
def __init__(self, parent):
TK.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
#self.parent.title("Smartstop Kiosk")
software = ('Microsoft', 'Abode', 'SAP', 'Lotus Notes', 'Jive','Cisco', 'Chrome')
hardware = ("Computer", "Charger", " USB Headset", "Keyboard", "Mouse", "Webcam")
microsoft = ("Outlook", "Lync", "Word", "Excel", "PowerPoint", "Visio", "Project", "Publisher")
adobe = ("Adobe Reader", "Java", "Flash", "Adobe Pro")
cisco = ('IP Communicator', '')
wireless = ("MYLOW", "Corporate", "Visitor", "MYLTW")
printers = ("2N", "2S", "3N", "3S", "4N", "4S", "5N", "5S")
mobile = ("Blackberry", "iPhone","iPad")
AD = ("Unlock", "Reset")
LocalTemplate = ('User Assistance with Software', 'User Assistance with Hardware', 'Mobile Device Support', 'Software Installation Request', 'Hardware Request', 'Wireless Connection', 'Password Reset/Username Unlocked',
'Add Printer')
def getchoice2():
print
# use same method to find out which of the second list is chosen, and carry on.
def getchoice():
seltext = ''
INDEX = Listbox1.curselection()
if INDEX == ('0',) or INDEX == ('4',):
seltext = software
if INDEX == ('1',) or INDEX == ('5',):
seltext = hardware
if INDEX == ('2',):
seltext = mobile
if INDEX == ('3',):
seltext = wireless
if INDEX == ('6',):
seltext = AD
if INDEX == ('7',):
seltext = printers
selectchoice.config(command = getchoice2)
for x in seltext:
Listbox2.insert(TK.END, x)
#creates font types
font = ('Times New Roman', 14, 'bold')
font2 = ('Times New Roman',12, 'bold')
#Creates Instructions for Users
stepOne = TK.LabelFrame(self, text=" 1. User Information: ", font = font2)
stepOne.grid(row=0, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
stepTwo = TK.LabelFrame(self, text="2. Select Program or Hardware Problems", font = font2)
stepTwo.grid(row=3, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
stepThree = TK.LabelFrame(self, text="Please Enter the Problem or Your Request:", font = font2)
stepThree.grid(row=6, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
#Creates Label for Users
Label1 = TK.Label(stepOne, text = "First Name", font = font)
Label1.grid(column = 2, row = 0, sticky = 'w', padx = (10, 10))
Label2 = TK.Label(stepOne, text = "Last Name", font = font)
Label2.grid(column = 4, row = 0, sticky = 'w', padx = (10, 10))
Label3 = TK.Label(stepOne, text = "Mylan UserName", font = font)
Label3.grid(column = 6, row = 0, sticky = 'w', padx = (10, 10))
#Creates Entry Box for User input
self.entry = TK.Entry(stepOne, width = 30)
self.entry.grid(column = 2, row = 1,padx = (10, 10))
entry2 = TK.Entry(stepOne, width = 30)
entry2.grid(column = 4, row = 1,padx = (10, 10))
entry3 = TK.Entry(stepOne, width = 30)
entry3.grid(column = 6, row = 1, columnspan = 2, padx = (10, 10))
#Creates User Input Box
Usertext = TK.Text(stepThree, height = 10, width = 110, font = font)
Usertext.grid(column = 2, row = 15, padx = (10, 10))
#Creates List Boxes
Listbox1 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font)
Listbox1.grid(column = 2, row = 1, padx = (10, 10))
for i in LocalTemplate:
Listbox1.insert(TK.END, i)
Listbox2 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font)
Listbox2.grid(column = 4, row = 1, padx = (10, 10))
Listbox3 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font)
Listbox3.grid(column = 6, row = 1, padx = (10, 10))
#Creates Buttons
selectchoice = TK.Button(self, text = 'select my choice', font = font2, command = getchoice)
selectchoice.grid(row = 5, column = 1, sticky = 'we', padx = (10, 10))
Submit = TK.Button(self, text = 'Submit', font = font2)
Submit.grid(column = 3, row = 10, sticky = 'we', padx = (10, 10))
Startover = TK.Button(self, text = 'Cancel', font = font2)
Startover.grid(column = 4, row = 10, sticky = 'we', padx = (10, 10))
def main():
app = smartstopkiosk_tk(None)
app.geometry("1300x768")
app.mainloop()
if __name__ == "__main__":
main()
This line is the problem, though the error message probably told you that:
value = widget.get(selection[0])
You have no variable named selection. Perhaps you meant to use index[0] instead of selection[0], since you set index on the immediately preceeding line?
Of course, you'll have a similar problem with widget which is also undefined.
I am trying to calculate a formula and display its output as a table in TKinter. Since it is not working, I am just trying to get a simple result and print it to a canvas widget. When this gets working I will do the entire loan formula. As it is I get no output in the GUI or in the console.
Is this even possible to place the result of a calculation as text in canvas.create_text?
from tkinter import * # Import tkinter
width = 500
height = 500
class MainGUI:
def __init__(self):
window = Tk() # Create a window
window.title(" Loan Schedule ") # Set title
frame1 = Frame(window)
frame1.grid(row = 1, column = 1)
Label(frame1, text = " Loan Amount ").grid(row = 1, column = 1, sticky = W)
self.v1 = StringVar()
Entry(frame1, textvariable = self.v1, justify = RIGHT).grid(row = 1, column = 2)
Label(frame1, text = " Years ").grid(row = 1, column = 3, sticky = W)
self.v2 = StringVar()
Entry(frame1, textvariable = self.v2, justify = RIGHT).grid(row = 1, column = 4)
btCalculate = Button(frame1, text = " Calculate ", command = self.calculate()).grid(row = 1, column = 5, sticky = E)
frame2 = Frame(window)
frame2.grid(row = 2, column = 1)
self.canvas = Canvas(frame2, width = width, height = height, bg = "white")
self.canvas.pack()
self.canvas.create_text(25, 25, text = self.calculate(), tags = "text")
window.mainloop() # Create an event loop
def calculate(self):
result = self.v1.get() + self.v2.get()
print(result)
return result
MainGUI()
command require function name without ()
command = self.calculate
so now it works
from tkinter import * # Import tkinter
width = 500
height = 500
class MainGUI:
def __init__(self):
window = Tk() # Create a window
window.title(" Loan Schedule ") # Set title
frame1 = Frame(window)
frame1.grid(row = 1, column = 1)
Label(frame1, text = " Loan Amount ").grid(row = 1, column = 1, sticky = W)
self.v1 = StringVar()
Entry(frame1, textvariable = self.v1, justify = RIGHT).grid(row = 1, column = 2)
Label(frame1, text = " Years ").grid(row = 1, column = 3, sticky = W)
self.v2 = StringVar()
Entry(frame1, textvariable = self.v2, justify = RIGHT).grid(row = 1, column = 4)
btCalculate = Button(frame1, text = " Calculate ", command = self.calculate).grid(row = 1, column = 5, sticky = E)
frame2 = Frame(window)
frame2.grid(row = 2, column = 1)
self.canvas = Canvas(frame2, width = width, height = height, bg = "white")
self.canvas.pack()
self.canvas.create_text(55, 10, text = self.add_text(), tags = "text")
window.mainloop() # Create an event loop
def calculate(self):
result = int(self.v1.get()) + int(self.v2.get())
self.canvas.create_text(25, 25, text = result, tags = "text")
print(result)
return result
def add_text(self):
return "HELLO WORLD"
MainGUI()
by the way: line below means - run self.calculate() and result assign to command
command = self.calculate()