I am losing my peanuts here. I am trying to clear two label values but i get an error
AttributeError: 'Label' object has no attribute 'delete'
basically if i were to click the calculate subtotal button then click the divide total button. I get my intended values. Now if I were to click on the clear values button i get an error. Literally shaking my head as I type this. Anyone care to explain why this is the case?
try:
import Tkinter as tk
except:
import tkinter as tk
class GetInterfaceValues():
def __init__(self):
self.root = tk.Tk()
self.totalValue = tk.StringVar()
self.root.geometry('500x200')
self.calculateButton = tk.Button(self.root,
text='Calculate Subtotal',
command=self.getSubtotals)
self.divideTotalButton = tk.Button(self.root,
text='Divide total',
command=self.divide)
self.textInputBox = tk.Text(self.root, relief=tk.RIDGE, height=1, width = 6, borderwidth=2)
self.firstLabel = tk.Label(self.root, text="This is the subtotal:")
self.secondLabel = tk.Label(self.root, text="This is the Divide Total:")
self.clearTotalButton = tk.Button(self.root, text='clear the values',command = self.clear)
self.firstLabel.pack(side="bottom")
self.secondLabel.pack(side="bottom")
self.textInputBox.pack()
self.calculateButton.pack()
self.divideTotalButton.pack()
self.clearTotalButton.pack()
self.root.mainloop()
def getTextInput(self):
result = self.textInputBox.get("1.0", "end")
return result
def getSubtotals(self):
userValue = int(self.getTextInput())
self.firstLabel["text"] = self.firstLabel["text"] + str(userValue * 5)
def divide(self):
userValue = int(self.getTextInput())
self.secondLabel["text"] = self.secondLabel["text"] + str(userValue / 10)
def clear(self):
self.firstLabel["text"] = self.firstLabel.delete("1.0","end")
app = GetInterfaceValues()
try:
import Tkinter as tk
except:
import tkinter as tk
class GetInterfaceValues():
def __init__(self):
self.root = tk.Tk()
self.totalValue = tk.StringVar()
self.root.geometry('500x200')
self.calculateButton = tk.Button(self.root,
text='Calculate Subtotal',
command=self.getSubtotals)
self.divideTotalButton = tk.Button(self.root,
text='Divide total',
command=self.divide)
self.textInputBox = tk.Text(self.root, relief=tk.RIDGE, height=1, width = 6, borderwidth=2)
self.firstLabelDefault = "This is the subtotal:"
self.secondLabelDefault = "This is the Divide Total:"
self.firstLabel = tk.Label(self.root, text=self.firstLabelDefault)
self.secondLabel = tk.Label(self.root, text=self.secondLabelDefault)
self.clearTotalButton = tk.Button(self.root, text='clear the values',command = self.clear)
self.firstLabel.pack(side="bottom")
self.secondLabel.pack(side="bottom")
self.textInputBox.pack()
self.calculateButton.pack()
self.divideTotalButton.pack()
self.clearTotalButton.pack()
self.root.mainloop()
def getTextInput(self):
result = self.textInputBox.get("1.0", "end")
return result
def getSubtotals(self):
userValue = int(self.getTextInput())
self.firstLabel["text"] = self.firstLabel["text"] + str(userValue * 5)
def divide(self):
userValue = int(self.getTextInput())
self.secondLabel["text"] = self.secondLabel["text"] + str(userValue / 10)
def clear(self):
self.firstLabel["text"] = self.firstLabelDefault
self.secondLabel["text"] = self.secondLabelDefault
self.textInputBox.delete("1.0", "end")
app = GetInterfaceValues()
You may have confused the methods of tkinter.Text and tkinter.Label. The method you called was tkinter.label.delete, which is not defined (does not exist), however it does exist for the tkinter.Text. Therefore, the only way to 'reset' would be to change the text attribute of the tkinter.Labels back to a 'default' string. It would perhaps be more appropriate to use another widget instead.
Related
This is to be a desktop application for opening multiple small databases and running queries on them. So far I've written some code for opening forms as necessary. Is this a good way to do it? Also - the code shown opens two copies of each form - what am I doing wrong? It's my first attempt at Python and I'm a rudimentary programmer so simple answers would be of most help please. TIA (Python 3.9.6)
link_1.py
from tkinter import Tk
import link_2
root = Tk()
class ClassLink_1:
#if another function in the class is called, the __init__ function is run at start up
def __init__(self):
print("in link_1 __init__ instruction")
#the call_function function can be called at start up, or not, and will act accordingly
def call_function(self):
print("in call_function")
#the line below is run at start up whether or not another function in the class is called
print("in link_1")
root.withdraw() #hides the blank form at start up
#if __name__ == "__main__":
#the line below shows the link_2 form, whether or not the if __name__==__main__ condition is used as its condition
link_2.ClassLink_2(root).__init__(root)
#link_3.ClassLink_3(root).__init__(root)
#the line below runs call_function on start up to print text
ClassLink_1().call_function()
root.mainloop()
link_2.py
from tkinter import Tk, Button
from tkinter import * #for Toplevel
import link_3
root = Tk()
class ClassLink_2:
def __init__(self, master):
self.openNewWindow()
def openNewWindow(self):
newWindow = Toplevel(root) #creates a top level widget with the parent root (first parameter)
newWindow.title("Title opened from link_1")
newWindow.geometry("500x500")
label = Label(newWindow, text ="Opened from link_1").grid(row=1, column=1)
self.add_button = Button(newWindow, text="in ClassLink_2", command= self.do_add)
self.add_button.grid(row=3, column=1)
def do_add(self):
print("button pressed")
link_3.ClassLink_3(root).__init__(root)
root.withdraw() #hides the blank form at start up
link_3.py
from tkinter import Tk, Button
from tkinter import * #for Toplevel
root = Tk()
class ClassLink_3:
def __init__(self, master):
self.openNewWindow()
def openNewWindow(self):
newWindow = Toplevel(root) #creates a top level widget with the parent root (first parameter)
newWindow.title("Title opened from link_2")
newWindow.geometry("500x500")
label = Label(newWindow, text ="Opened from link_2").grid(row=1, column=1)
self.add_button = Button(newWindow, text="in ClassLink_3", command= self.do_add)
self.add_button.grid(row=3, column=1)
def do_add(self):
print("button pressed")
# link_4.ClassLink_4(root).__init__(root) this file has not yet been made
root.withdraw() #hides the blank form at start up
This is a proposed solution, can be expanded as needed. Constructive suggestions for improvement of the structure or code would be appreciated. TIA. I've left in the details in case they are of use to anyone.
link_1
from tkinter import Tk
import link_2
root = Tk()
class ClassLink_1:
def __init__(self):
print("in link_1 __init__ instruction")
root.withdraw() #hides the blank form at start up
link_2.ClassLink_2(root).openNewWindow(0)
root.mainloop()
link_2
from tkinter import Tk, Button
from tkinter import *
import link_3
root = Tk()
class ClassLink_2:
root.withdraw() #hides the blank form at start up
class_var_1 = 0
inst_var_1 = 0
def __init__(self, incoming_inst_var_1):
pass
def openNewWindow(self, inst_var_1_to_open):
newWindow = Toplevel(root)
newWindow.title("Title opened from link_1")
newWindow.geometry("500x500")
label = Label(newWindow, text ="Opened from link_1").grid(row=1, column=1)
self.add_button = Button(newWindow, text="in ClassLink_2", command= self.do_add)
self.add_button.grid(row=3, column=1)
self.add_button = Button(newWindow, text="increment class_var_1", command= self.inc_class_var_1)
self.add_button.grid(row=5, column=1)
self.inst_var_1 = inst_var_1_to_open
def do_add(self):
print("button pressed")
link_3.ClassLink_3(root).openNewWindow(0)
def inc_class_var_1(self):
ClassLink_2.class_var_1 += 2
self.inst_var_1 += 1
print("self.class_var_1 = " + (str)(self.class_var_1))
print("self.inst_var_1 = " + (str)(self.inst_var_1))
link_3
from tkinter import Tk, Button
from tkinter import *
from tkinter.ttk import Combobox
import tkinter.scrolledtext as tkscrolled
# import link_4 <filename of next form>
root = Tk()
class ClassLink_3:
class_var_1 = 0
inst_var_1 = 0
# ------------------------------- START CLASS CONTROLS -----------------------------
root.withdraw()
def __init__(self, incoming_inst_var_1):
pass
def openNewWindow(self, inst_var_1_to_open):
new_window = Toplevel(root)
self.widget_factory(new_window)
self.inst_var_1 = inst_var_1_to_open
def do_add(self):
print("button pressed")
# link_4.ClassLink_4(root).openNewWindow(0) # <filename of next form>
# ---------------------------------- END CLASS CONTROLS -----------------------------
# -------------------------------------- START CALCS --------------------------------------
def inc_class_var_1(self):
ClassLink_3.class_var_1 += 2
self.inst_var_1 += 1
print("self.class_var_1 = " + (str)(self.class_var_1))
print("self.inst_var_1 = " + (str)(self.inst_var_1))
# ---------------------------------------- END CALCS --------------------------------------
# ---------------------------------------- START FACTORY SHOPS-----------------------------------------
def widget_factory(self, new_window):
self.shop_window(new_window)
self.shop_labels(new_window)
self.shop_buttons(new_window)
self.shop_menu(new_window)
self.shop_listbox(new_window)
self.shop_combobox(new_window)
self.shop_radiobuttons(new_window)
self.shop_checkbuttons(new_window)
self.shop_entries(new_window)
self.shop_canvas(new_window)
self.shop_scale(new_window)
self.shop_scrollbars(new_window)
self.shop_textbox(new_window)
self.shop_menu(new_window)
pass
# ------------------------
def shop_window(self, new_window):
new_window.title("Title opened from link_2")
new_window.geometry("800x900")
def shop_labels(self, new_window):
self.label_1 = Label(new_window, text ="Opened from link_2").grid(row=1, column=1)
def shop_buttons(self, new_window):
self.button_1 = Button(new_window, text="in ClassLink_3", command= self.do_add)
self.button_1.grid(row=3, column=1)
self.button_2 = Button(new_window, text="increment class_var_1", command= self.inc_class_var_1)
self.button_2.grid(row=5, column=1)
def shop_listbox(self, new_window):
data=("one", "two", "three", "four")
self.listbox_1 = Listbox(new_window, height=5, selectmode='multiple')
for num in data:
self.listbox_1.insert(END,num)
self.listbox_1.grid(row=7, column=1)
def shop_combobox(self, new_window):
data=("one", "two", "three", "four")
self.combobox_1 = Combobox(new_window, values=data)
self.combobox_1.grid(row=8, column=3)
def shop_radiobuttons(self, new_window):
var_1 = IntVar()
var_1.set(1)
self.rad_btn_1 = Radiobutton(new_window, text="male", variable=var_1, value=1)
self.rad_btn_2 = Radiobutton(new_window, text="female", variable=var_1, value=2)
self.rad_btn_1.grid(row=9, column=1)
self.rad_btn_2.grid(row=9, column=2)
def shop_checkbuttons(self, new_window):
var_1 = IntVar()
var_2 = IntVar()
self.checkbtn_1 = Checkbutton(new_window, text = "Cricket", variable = var_1)
self.checkbtn_2 = Checkbutton(new_window, text = "Tennis", variable = var_2)
self.checkbtn_1.grid(row=10, column=1)
self.checkbtn_2.grid(row=10, column=2)
def shop_entries(self, new_window):
self.entry_1 = Entry(new_window, width = 20)
self.entry_1.insert(0,'Username')
self.entry_1.grid(row= 11, column=2)
self.entry_2 = Entry(new_window, width = 15)
self.entry_2.insert(0,'password')
self.entry_2.grid(row= 12, column=2)
#might want to place on a frame, see example https://coderslegacy.com/python/list-of-tkinter-widgets/
def shop_canvas(self, new_window):
canvas = Canvas(new_window, bg= 'white', width = 260,height = 260) #this is the background for the figure
# left and top of figure is from [x from left, y from top] of canvas right and bottom of figure from [x from left, y from top] of canvas
coordinates = 20, 50, 210, 230
arc = canvas.create_arc(coordinates, start=0, extent=250, fill="blue") #start is from E, extent is in degrees CCW
arc = canvas.create_arc(coordinates, start=250, extent=50, fill="red")
arc = canvas.create_arc(coordinates, start=300, extent=60, fill="yellow")
canvas.grid(row=2, column=1)
def shop_scale(self, new_window):
self.scale_1 = Scale(new_window, from_=0, to=10, orient=VERTICAL)
self.scale_1.grid(row=15, column=2)
self.scale_2 = Scale(new_window, from_=0, to=10, orient = HORIZONTAL)
self.scale_2.grid(row=15, column=3)
def shop_scrollbars(self, new_window):
self.scroll_vert = Scrollbar(new_window)
self.scroll_vert.grid(row=19, column=3)
self.listbox_3 = Listbox(new_window, yscrollcommand = self.scroll_vert.set )
for line in range(1, 100):
self.listbox_3.insert(END, "Number " + str(line))
self.listbox_3.grid(row=19 , column=2)
self.scroll_vert.config(command = self.listbox_3.yview)
def shop_textbox(self, new_window):
#make a frame with parent new_window
self.frame_textbox_1 = Frame(new_window)
self.frame_textbox_1.grid(row=1, column=5)
#make the textbox with parent frame
self.textbox_1 = Text(self.frame_textbox_1)
self.textbox_1.config(wrap=NONE, width=15, height=8) #width, height are characters x rows permitted wrap values should be WORD CHAR, or NONE
#make the X scrollbar with parent frame
self.scroll_text_horiz = Scrollbar(self.frame_textbox_1, orient="horizontal")
self.scroll_text_horiz.config(command = self.textbox_1.xview)
#make the Y scrollbar with parent frame
self.scroll_text_vert = Scrollbar(self.frame_textbox_1, orient="vertical")
self.scroll_text_vert.config(command = self.textbox_1.yview)
#pack the scrollbars before the texbox to allow them to fill the frame width and height
self.scroll_text_horiz.pack(side=BOTTOM, fill=X)
self.scroll_text_vert.pack(side=RIGHT, fill=Y)
self.textbox_1.pack(fill="y")
#connect the scrollbars to the textbox
self.textbox_1["xscrollcommand"] = self.scroll_text_horiz.set
self.textbox_1["yscrollcommand"] = self.scroll_text_vert.set
message = "the quick brown fox"
self.textbox_1.insert(1.1, message)
# ----------------------------------------------
def shop_menu(self, new_window):
print("in shop menu1")
menubar = Menu(new_window)
print("in shop_menu2")
file = Menu(menubar, tearoff=0)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as...")
file.add_command(label="Close")
file.add_separator()
file.add_command(label="Exit", command=new_window.quit)
menubar.add_cascade(label="File", menu=file)
edit = Menu(menubar, tearoff=0)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
edit.add_command(label="Delete")
edit.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=edit)
help = Menu(menubar, tearoff=0)
help.add_command(label="About")
menubar.add_cascade(label="Help", menu=help)
new_window.config(menu=menubar)
print("in shop menu3")
# --------------------------------------- END FACTORY SHOPS---------------------------------------
from tkinter import Tk, Frame, Button, Label, Entry, ttk, StringVar, messagebox
import datetime
# Main Window
class WINDOW(Tk):
def __init__(self, master):
Tk.__init__(self, master)
self.master = master
frame1 = Frame1(self)
frame1.grid(row=0,column=0)
class Frame1(Frame):
def __init__(self, master):
Frame.__init__(self, master,height=master.winfo_screenheight(),
width=master.winfo_screenwidth())
self.master = master
var = StringVar()
label = Label(self, text="DATA", font=('calibre', 10, 'bold'))
entry = Entry(self, textvariable=var,width=10, font=('calibre', 10, 'normal'))
label.grid(row=0, column=1)
entry.grid(row=1, column=1)
# Log data sheet
NewTree = ttk.Treeview(self, height=23, columns=("Time","Time Diff", "DATA"))
NewTree['show'] = 'headings'
NewTree['columns'] = ("Time","Time Diff", "DATA")
NewTree.column("Time", width=180)
NewTree.column("Time Diff", width=150)
NewTree.column("DATA", width=150)
NewTree.heading("Time",text='TIME')
NewTree.heading("Time Diff",text='TimeDIfference')
NewTree.heading("DATA",text='DATA')
NewTree.grid(row=5, columnspan=4)
def insert_data():
if entry.get()=='':
messagebox.showerror("","Please insert data")
else:
NewTree.insert('', 'end',values=(datetime.datetime.now(),"",entry.get()))
entry.delete(0, 'end')
submit_button = Button(self, text="SUBMIT", command=insert_data)
submit_button.grid(row=3, column=4)
root = WINDOW(None)
root.geometry(f'{root.winfo_screenwidth()}x{root.winfo_screenheight()}')
root.title("ADD DATA")
root.mainloop()
Please refer the image:
there is time difference between submitting the 11 and 12, 12 and 24 and so on.
i need to calculate that time and want to add in time difference column.
it is like how much time it takes to add next data
I am not quite sure with what you are comparing the value, but my guess is the previous time.
you can use relativedelta to get the time difference.
In the beginning, assign both currenttime = previoustime = datetime.datetime.now(). Then when ever the user calls update again assign currenttime=datetime.datetime.now() find the time difference, display the data in the treeview and then update previoustime=currenttime
Here is an example.
from tkinter import ttk,Tk
import datetime
from dateutil.relativedelta import relativedelta
def diff(t_a, t_b):
t_diff = relativedelta(t_b, t_a)
return ' {h}h {m}m {s}s {ms}ms'.format(h=abs(t_diff.hours), m=abs(t_diff.minutes), s=abs(t_diff.seconds), ms=abs(round(t_diff.microseconds*0.001, 2)))
def update():
global currenttime, previoustime, timediff
currenttime= datetime.datetime.now()
timediff = diff(currenttime, previoustime)
curr = tree.insert('','end',values=(currenttime,timediff))
previoustime = currenttime
tree.selection_set(curr)
tree.see(curr)
root = Tk()
tree = ttk.Treeview(root, height = 20, columns = 2)
tree['show']='headings'
tree['columns'] = ('one','two')
tree.column('one', width = 250)
tree.column('two', width = 250)
tree.heading('one', text = 'TIME')
tree.heading('two', text = 'TIME Difference')
tree.grid(row = 0, column = 0)
currenttime= datetime.datetime.now()
previoustime= currenttime
timeBtn = ttk.Button(root, text='Update', command=update)
timeBtn.grid(row=1, column=0)
root.geometry("500x500")
root.mainloop()
Update after OP's request:
class Frame1(Frame):
def __init__(self, master):
...
NewTree.grid(row=5, columnspan=4)
self.currenttime = None
self.previoustime = None
self.timediff = None
def diff(t_a, t_b):
t_diff = relativedelta(t_b, t_a)
return ' {h}h {m}m {s}s {ms}ms'.format(h=abs(t_diff.hours), m=abs(t_diff.minutes), s=abs(t_diff.seconds), ms=abs(round(t_diff.microseconds*0.001, 2)))
def update_time():
if entry.get()=='':
messagebox.showerror("","Please insert data")
else:
self.currenttime= datetime.datetime.now()
if self.previoustime is None:
self.previoustime = self.currenttime
self.timediff = diff(self.currenttime, self.previoustime)
curr = NewTree.insert('','end',values=(datetime.datetime.now(), self.timediff ,entry.get()))
self.previoustime = self.currenttime
NewTree.selection_set(curr)
NewTree.see(curr)
submit_button = Button(self, text="SUBMIT", command=update_time)
...
I have two sets of code. One works the other doesn't and I don't know why. In the first set of code I am storing my application into a class and referencing everything there. In the second one I am storing everything in the main function and using it there.
The main details of the problem are here:
I am using a entry widget and a button widget which is initially disabled. I want the button's state to be normal when there is text in the entry widgets text field.
I have searched online for many answers to the first set of code but the closest I have gotten is the second set of code. First I tried to integrate it into my code however that did not work. So what I did was take the code and strip it to the bare minimum and put everything into the main function.
The main difference between the two is one is in a class and the other is in the main function.
import tkinter as tk
class Aplication(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
self.grid()
self.button_clicks = 0
something = tk.StringVar()
button3 = tk.Button(self, text="Print entry", padx = 10, height = 2, bg = "blue", state = "disabled")
entry = tk.Entry(self, textvariable = something)
something.trace('w', self.ActivateButton) #need this to always be exsecuted
entry.grid(column = 2, row = 1)
button3["command"] = lambda: self.PrintEntry(entry.get())
button3.grid(padx = 10, pady = 10)
def PrintEntry (self, entry):
print(entry)
def ActivateButton(self, *_):
if something.get():
button3['state'] = "normal"
else:
button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Aplication(top)
top.mainloop()
def PrintEntry (entry):
print(entry)
def ActivateButton(*_):
if entry.get():
button3['state'] = "normal"
else:
button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
something = tk.StringVar()
button3 = tk.Button(top, text="Print entry", padx = 10, height = 2, bg = "blue", state = "disabled")
entry = tk.Entry(top, textvariable = something, bd = 2)
something.trace('w', ActivateButton)
entry.grid(column = 2, row = 3)
button3["command"] = lambda: PrintEntry(entry.get())
button3.grid(row = 3, column = 1, padx = 10, pady = 10)
top.mainloop()
There are no error messages; however you will find in the first set, the button's state never gets set to normal. Is there a way to do this for the first one? What is the difference between the two that makes it to where I can't enable the button in the first one if it is impossible?
You have to use self. to create class variables so they will be accesible in all methods in class. Currently you create local variable something in __init__ and it is deleted when Python ends __init__ - so finally it removes something.trace() and it doesn't check value in entry.
In this code I use self.something and self.button3 and it works correctly.
import tkinter as tk
class Aplication(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
self.grid()
self.button_clicks = 0
self.something = tk.StringVar()
self.button3 = tk.Button(self, text="Print entry", padx = 10, height = 2, bg = "blue", state = "disabled")
entry = tk.Entry(self, textvariable = self.something)
self.something.trace('w', self.ActivateButton) #need this to always be exsecuted
entry.grid(column = 2, row = 1)
self.button3["command"] = lambda: self.PrintEntry(entry.get())
self.button3.grid(padx = 10, pady = 10)
def PrintEntry (self, entry):
print(entry)
def ActivateButton(self, *_):
if self.something.get():
self.button3['state'] = "normal"
else:
self.button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Aplication(top)
top.mainloop()
EDIT: the same little different - without lambda. I use self.entry (as class variable) directly in print_entry.
import tkinter as tk
class Aplication(tk.Frame):
def __init__(self, master):
super().__init__(master)#tk.Frame.__init__(self,master)
self.grid()
self.button_clicks = 0
self.something = tk.StringVar()
self.entry = tk.Entry(self, textvariable=self.something)
self.entry.grid(column=2, row=1)
self.button3 = tk.Button(self, command=self.print_entry, text="Print entry", padx=10, height=2, bg="blue", state="disabled")
self.button3.grid(padx=10, pady=10)
self.something.trace('w', self.activate_button) #need this to always be exsecuted
def print_entry(self):
print(self.entry.get())
def activate_button(self, *_):
if self.something.get():
self.button3['state'] = "normal"
else:
self.button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Aplication(top)
top.mainloop()
As pointed out by #furas you need to prefix your variables in the constructor method def __init__ with self. so you can access attributes and methods since it literally represents an instance of the class. This link explains it in more detail https://stackoverflow.com/a/2709832/7585554.
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.grid()
self.button_clicks = 0
self.something = tk.StringVar()
self.button3 = tk.Button(self, text="Print entry", padx=10, height=2, bg="blue", state="disabled")
self.entry = tk.Entry(self, textvariable=self.something)
self.something.trace('w', self.ActivateButton)
self.entry.grid(column=2, row=1)
self.button3["command"] = lambda: self.PrintEntry()
self.button3.grid(padx=10, pady=10)
def PrintEntry (self):
print(self.entry.get())
def ActivateButton(self, *_):
if self.something.get():
self.button3['state'] = "normal"
else:
self.button3['state'] = "disabled"
if __name__ == '__main__':
top = tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Application(top)
top.mainloop()
A simple quiz game
I got this code and I need scrollbars, I tried to search how to add it on stackoverflow (ScrolledWindow with tix...) but I still can't get something that works properly. Could someone help me?
from tkinter import *
from random import randint
root = Tk()
root.title("Quiz")
root.geometry("400x300")
class Window:
def __init__(self, question, answer):
self.text = [question, answer]
self.createLabel()
# self.createText()
self.createEntry()
self.createButton()
def retrieve_input(self):
# inputValue = self.textBox.get("1.0", "end-1c")
# print(inputValue)
if self.mystring.get() == self.text[1]:
print("Esatto. è " + self.text[1])
self.left['text'] = "Esatto"
def createLabel(self):
self.labelframe = LabelFrame(root, text="Domanda:")
self.labelframe.pack(fill="both", expand="yes")
self.left = Label(self.labelframe, text=self.text[0])
self.left.pack()
def createText(self):
self.textBox = Text(height=1)
self.textBox.pack()
def createEntry(self):
self.mystring = StringVar()
self.myentry = Entry(root, textvariable=self.mystring).pack()
def createButton(self):
self.but = Button(text="Click", command=self.retrieve_input)
self.but.pack()
for i in range(10):
one = randint(1, 10)
two = randint(1, 10)
Window("Quanto fa " + str(one) + "+" + str(two) + "?", str(one + two))
root.mainloop()
output
With ScrolledFrame it can look like this
I renamed Window into Question because it makes more sense
I use self.question and self.answer instead of self.text = [question, answer] to make it more readable.
I put classes and functions before root = tk.Tk() to make it more readable.
I use import tkinter as tk instead of from tkinter import * to make it more readable.
Question gets inner frame from ScrolledFrame and use as parent for LabelFrame. Other widgets use labelframe as parent.
BTW: you had entry = Entry(..).pack() which assign None to entry because pack()/grid()/place() returns None. I put pack() in next line and now I can get text directly from Entry (without StringVar)
Code
import tkinter as tk
from random import randint
# --- classes ---
class ScrolledFrame(tk.Frame):
def __init__(self, parent, vertical=True, horizontal=False):
super().__init__(parent)
# canvas for inner frame
self._canvas = tk.Canvas(self)
self._canvas.grid(row=0, column=0, sticky='news') # changed
# create right scrollbar and connect to canvas Y
self._vertical_bar = tk.Scrollbar(self, orient='vertical', command=self._canvas.yview)
if vertical:
self._vertical_bar.grid(row=0, column=1, sticky='ns')
self._canvas.configure(yscrollcommand=self._vertical_bar.set)
# create bottom scrollbar and connect to canvas X
self._horizontal_bar = tk.Scrollbar(self, orient='horizontal', command=self._canvas.xview)
if horizontal:
self._horizontal_bar.grid(row=1, column=0, sticky='we')
self._canvas.configure(xscrollcommand=self._horizontal_bar.set)
# inner frame for widgets
self.inner = tk.Frame(self._canvas, bg='red')
self._window = self._canvas.create_window((0, 0), window=self.inner, anchor='nw')
# autoresize inner frame
self.columnconfigure(0, weight=1) # changed
self.rowconfigure(0, weight=1) # changed
# resize when configure changed
self.inner.bind('<Configure>', self.resize)
self._canvas.bind('<Configure>', self.frame_width)
def frame_width(self, event):
# resize inner frame to canvas size
canvas_width = event.width
self._canvas.itemconfig(self._window, width = canvas_width)
def resize(self, event=None):
self._canvas.configure(scrollregion=self._canvas.bbox('all'))
class Question:
def __init__(self, parent, question, answer):
self.parent = parent
self.question = question
self.answer = answer
self.create_widgets()
def get_input(self):
value = self.entry.get()
print('value:', value)
if value == self.answer:
print("Esatto. è " + self.answer)
self.label['text'] = "Esatto"
def create_widgets(self):
self.labelframe = tk.LabelFrame(self.parent, text="Domanda:")
self.labelframe.pack(fill="both", expand=True)
self.label = tk.Label(self.labelframe, text=self.question)
self.label.pack(expand=True, fill='both')
self.entry = tk.Entry(self.labelframe)
self.entry.pack()
self.button = tk.Button(self.labelframe, text="Click", command=self.get_input)
self.button.pack()
# --- main ---
root = tk.Tk()
root.title("Quiz")
root.geometry("400x300")
window = ScrolledFrame(root)
window.pack(expand=True, fill='both')
for i in range(10):
one = randint(1, 10)
two = randint(1, 10)
Question(window.inner, "Quanto fa {} + {} ?".format(one, two), str(one + two))
root.mainloop()
I'm trying to do a simple GUI in Tkinter. I have a Listbox there and I want the user to know what kind of data is in the Listbox so I want to set a label for it (upper).
The problem is that when I set a label, this Listbox disappears.
l = Label(multiple_choose_days_listbox, textvariable=label_day_listbox , anchor=NW, justify='center')
l.pack()
The solution is probably obvious but I'm new in Tkinter.
Do you have any advices?
import Tkinter
import tkSimpleDialog
from Tkinter import *
import db
import ttkcalendar
class CalendarDialog(tkSimpleDialog.Dialog):
"""Dialog box that displays a calendar and returns the selected date"""
def __init__(self, master):
self.calendar = ttkcalendar.Calendar(master)
# self.calendar.pack()
#property
def result(self):
return self.calendar.selection
def pack(self,**kwargs):
self.calendar.pack(**kwargs)
states_list = db.get_states()
bought_days_before_list = db.get_bought_days_before()
multiple_choose_length_of_trips_list = db.get_lengths_of_trips()
def main():
root = Tkinter.Tk()
root.wm_title("CalendarDialog Demo")
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.geometry("1000x500")
top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
cd_1 = CalendarDialog(root)
cd_2 = CalendarDialog(root)
cd_1.pack(in_=top, side=LEFT)
cd_2.pack(in_=top, side=LEFT)
multiple_choose_states_listbox = Listbox(root, selectmode=EXTENDED)
multiple_choose_states_listbox.grid(row=20, columns=1)
multiple_choose_days_listbox = Listbox(root, selectmode=EXTENDED)
multiple_choose_length_of_trips_list_listbox = Listbox(root, selectmode=EXTENDED)
label_day_listbox = StringVar()
label_day_listbox.set("LABEL")
l = Label(multiple_choose_days_listbox, textvariable=label_day_listbox , anchor=NW, justify='center')
l.pack()
multiple_choose_days_listbox.pack(in_=top,side=LEFT)
multiple_choose_states_listbox.pack()
multiple_choose_length_of_trips_list_listbox.pack()
for item in states_list:
multiple_choose_states_listbox.insert(END, item)
for item in bought_days_before_list:
multiple_choose_days_listbox.insert(END, item)
def get_result_dict():
date_from = cd_1.result
date_to = cd_2.result
states = [states_list[i] for i in multiple_choose_states_listbox.curselection()]
bought_days_before = [bought_days_before_list[i] for i in multiple_choose_days_listbox.curselection()]
length_of_trip = [states_list[i] for i in multiple_choose_length_of_trips_list.curselection()]
res_dict = {
'date_from': date_from,
'date_to': date_to,
'states': states,
'bought_days_before': bought_days_before,
'length_of_trip': length_of_trip,
}
return res_dict
button = Tkinter.Button(root, text="Generate", command=get_result_dict)
button.pack()
root.update()
root.mainloop()
if __name__ == "__main__":
main()
It should be
l = Tkinter.Label(root, text="Label")