how to access the output from clicking the recommendation button? - python

i am making a movie recommendation GUI with the help of a dataset. For that, i need the value selected from the list by clickng on the recommedation button but i am not able to figure out to do so.
from tkinter import *
import pandas as pd
window = Tk()
window.config(background = 'white')
window.geometry('700x500')
title = Label(text = 'Movie Recommendation', font = ('times new roman',20),background = 'white')
title.pack()
label = Label(text = '**********************', font = ('times new roman',17),background = 'white' )
label.pack()
label = Label(text = 'Movie List :', font = ('times new roman',17),background = 'white' )
label.place(x = 5, y =66)
scroll_bar = Scrollbar(window)
scroll_bar.pack(side = RIGHT, fill = Y)
list = Listbox(window, yscrollcommand = scroll_bar.set, height = 21, width = 40, background = 'white', foreground = 'black' )
for i in df['title']:
list.insert(END,i)
list.place(x=5,y=105)
rec = Label(text = 'Recommended Movies :', font = ('times new roman',17),background = 'white')
rec.place(x = 300, y = 66)
def selected_item():
print(list.get(ANCHOR))
# selected = Label(text = selected_item(),font = ('times new roman',15))
# selected.place(x=300,y = 96)
def movie():
button = Button(text = "Recommendation",foreground='black', command = selected_item,background = 'white')
button.place(x=5,y=460)
movie()
window.mainloop()
this is my code.
i tried everything i could think of but i am new to coding. thank you.

Related

Can't modify global variable in python

from tkinter import *
import time
check = False
window = Tk()
window.geometry("1920x1080")
def typeTime():
hour = int(time.strftime("%H"))
minute = int(time.strftime("%M"))
second = int(time.strftime("%S"))
hourInput2 = int(hourInput.get())
minuteInput2 = int(minuteInput.get())
secondInput2 = int(secondInput.get())
if(hour == hourInput2 and minute == minuteInput2 and second == secondInput2):
print("now")
global check
check = True
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20)).place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20)).place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20)).place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10), command = typeTime)
submit.place(x = 100, y = 100)
if check == True:
print("Pressed")
submit.config(relief = SUNKEN)
window.mainloop()
I'm trying to make a button to stay pressed, so I tried to make this happens with a global variable. The variable check is initially False, but when typeTime() is called via the submit object it should change its value in True and when check will be tested later to keep my button pressed using config method.
What am I doing wrong, as neither the button is still pressed nor the message "Pressed" is displayed in the console ?
The window.mainloop() is the internal loop inside object window, not in your script so that is why it didn't work. You need to add the action inside the function typeTime:
from tkinter import *
import time
if __name__=='__main__':
check = False
window = Tk()
window.geometry("1920x1080")
def typeTime(button):
hour = int(time.strftime("%H"))
minute = int(time.strftime("%M"))
second = int(time.strftime("%S"))
hourInput2 = int(hourInput.get())
minuteInput2 = int(minuteInput.get())
secondInput2 = int(secondInput.get())
if(hour == hourInput2 and minute == minuteInput2 and second == secondInput2):
print("now")
# global check
# check = True
print('Pressed')
button.config(relief=SUNKEN)
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20)).place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20)).place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20)).place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10))
submit.config(command = lambda submit=submit:typeTime(submit))
submit.place(x = 100, y = 100)
# if check == True:
# print("Pressed")
# submit.config(relief = SUNKEN)
window.mainloop()

Why do the tkinter windows open when the program runs and not when the buttons are pressed?

I am trying to code a journal using tkinter. I have used different scripts for each part of the journal (main menu, morning entry and evening entry). I have set it so that the windows for morning and evening entry only open when their respective buttons are pressed on the main menu and this works well but when I run the programs both windows open automatically without me pressing anything. This happens before the main menu window is even opened. I have checked through the code and not noticed anything that can be causing this. Below is my code for the main menu and for the morning menu (evening is the same as morning menu with variables changed).
Main Menu
#importing libraries
import tkinter as tk
from tkinter import *
from Morning import *
from Evening import *
def StartUp():
Start = tk.Tk()
#Start Window Title and Size
Start.title("Journal")
Start.geometry("600x500")
Start["bg"] = "white"
#header Box
header = Label(Start, text = "Journal",
height = 1,
width = 7,
font = ("Comic Sans", 16, "bold"),
bg = "white")
header.pack(side = TOP)
#Buttons
morning_button = Button(Start, height = 2,
width = 20,
text = "Morning",
bg = "white",
command = lambda : morningwindow(),
)
morning_button.pack(side = LEFT)
evening_button = Button(Start, height = 2,
width = 20,
text = "Evening",
bg = "white",
command = lambda : eveningwindow())
evening_button.pack(side = RIGHT)
wklyQs_button = Button(Start, height = 2,
width = 30,
text = "Weekly Questions",
bg = "white")
wklyQs_button.pack()
mnthlycheck_button = Button(Start, height = 2,
width = 30,
text = "Monthly Check In",
bg = "white")
mnthlycheck_button.pack()
quit_button = Button(Start, height = 2,
width = 20,
text = "QUIT",
bg = "white",
command = quit)
quit_button.pack(side = BOTTOM)
#mainloop
Start.mainloop()
StartUp()
Morning Entry
#importing libraries
import tkinter as tk
from tkinter import *
from datetime import *
def morningwindow():
Morning = tk.Tk()
#Morning Window Title and Size
Morning.title("Morning")
Morning.geometry("1400x700")
Morning["bg"] = "#97e8fc"
#header Box
header = Label(Morning, text = "Morning Entry",
height = 1,
width = 20,
font = ("Comic Sans", 16, "bold"),
bg = "white")
header.pack(anchor=N)
#date Box
today = date.today()
format_date = "Today's date", today.strftime("%b-%d-%y")
date_text = Label(Morning, text = (format_date),
height = 1,
width = 20,
font = ("Comic Sans", 16, "bold"),
bg = "white"
)
date_text.pack(anchor=NE, padx = 10, pady = 10)
##Entries
#Grateful Entry
grateful_text = Text(Morning, height = 4, width = 160)
grateful_text.insert(END,
"What are you grateful for? \n1.\n2.\n3.")
grateful_text.pack()
#Good Day Entry
goodday_text = Text(Morning, height = 4, width = 160)
goodday_text.insert(END,
"This is how I'll make today great:")
goodday_text.pack(pady = 10)
#Positive Affirmation Entry
affirm_text = Text(Morning, height = 4, width = 160)
affirm_text.insert(END,
"Positive Affirmation \n")
affirm_text.pack()
##Buttons
#back Button
back_button = Button(Morning, height = 2,
width = 20,
text = "BACK",
bg = "white",
command = lambda : Morning.destroy())
back_button.pack(side = BOTTOM)
#Submit Button
submit_button = Button(Morning, height = 2,
width = 20,
text = "SUBMIT",
bg = "white",
)
submit_button.pack(side = BOTTOM, pady = 10)
#mainloop
Morning.mainloop()

Macosx button not appearing in child window

My first post, so I apologize as I learn to use this. Also, I am sure my code is very jumbled and rudimentary, but I am not here to get someone to re-write my code. I have a question about how a widget is behaving and an error code I am getting.
I currently have two frames in a mainWindow, and when you click a button labeled "New", a small window (newWindow) pops up that has a registration form, and a button labeled "Register" to write the form data to a .txt file.
The problem I am having is that if I do not define where the button should go, it appears in the mainWindow. If I specify for it to appear in the newWindow, it does not appear and throws me the following error:
File
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/init.py",
line 1507, in nametowidget
w = w.children[n] KeyError: '!button2'
Here is my code (get ready for a mess!):
from tkinter import *
from tkinter import ttk
from time import strftime
from tkmacosx import SFrame, Button
import tkinter as tk
mainWindow = Tk()
mainWindow.title('BATCO Main')
def createWindow():
#When createWindow() is called, the following code is executed, starting with creating the window object itself, which is set to the Tk() widget caller
newWindow = Tk()
# Add title of window
newWindow.title('Patron Entry')
# Set from and geometry of the Button (widthxheight+XPOS+YPOS)
newWindow.geometry('500x350+100+200')
# THIS PART CREATES AND APPENDS THE FILE FOR STORING NAMES:
def saveInfo():
# Defines Variables(3) for the "get" Method that will take from the StringVar Variables(2) above:
fname_info = fname.get()
mname_info = mname.get()
lname_info = lname.get()
suffix_info = suffix.get()
addr1_info = addr1.get()
addr2_info = addr2.get()
city_info = city.get()
state_info = state.get()
zipcode_info = zipcode.get()
print(fname_info, mname_info, lname_info, suffix_info, addr1_info, addr2_info, city_info, state_info, zipcode_info)
# This starts by opening the file 'patrons.txt' and assigns it to the file Variable. It uses the Arg 'a' which stands for Append, so it will add to the file.
file = open('patrons.txt', 'a')
# Once the file is open, write to it using the write method on the Variable file. The Write method accepts one Arg, so I have to use the + Operator to Concatenate text.
file.write (fname_info + ',')
file.write (mname_info + ',')
file.write (lname_info + ',')
file.write (suffix_info + ',')
file.write (addr1_info + ',')
file.write (addr2_info + ',')
file.write (city_info + ',')
file.write (state_info + ',')
file.write (zipcode_info + ';\n')
file.close ()
print (fname_info, 'has been registered')
# Clears the fields in the form
fname_entry.delete(0, END)
mname_entry.delete(0, END)
lname_entry.delete(0, END)
suffix_entry.delete(0, END)
addr1_entry.delete(0, END)
addr2_entry.delete(0, END)
city_entry.delete(0, END)
state_entry.delete(0, END)
zipcode_entry.delete(0, END)
# This code closes the window after entry
newWindow.destroy()
# End of saveInfo function
# Creating basic text that is formatted to be a header at the top of the window
lbl = Label(newWindow, text = 'Create Record', bg = 'light gray', fg = 'gray', width = '50', font = ('Arial', 17))
lbl.place(x=0, y=0)
# Defining Variables(1) for the text box labels:
fname_text = Label(newWindow, text = 'First Name', fg = 'gray', font = ('Arial', 12))
mname_text = Label(newWindow, text = 'Middle name', fg = 'gray', font = ('Arial', 12))
lname_text = Label(newWindow, text = 'Last Name', fg = 'gray', font = ('Arial', 12))
suffix_text = Label(newWindow, text = 'Suffix', fg = 'gray', font = ('Arial', 12))
addr1_text = Label(newWindow, text = 'Address 1', fg = 'gray', font = ('Arial', 12))
addr2_text = Label(newWindow, text = 'Address 2', fg = 'gray', font = ('Arial', 12))
city_text = Label(newWindow, text = 'City', fg = 'gray', font = ('Arial', 12))
state_text = Label(newWindow, text = 'State', fg = 'gray', font = ('Arial', 12))
zip_text = Label(newWindow, text = 'Zip', fg = 'gray', font = ('Arial', 12))
# Placing text box labels:
fname_text.place(x = 15, y = 50)
mname_text.place(x = 155, y = 50)
lname_text.place(x = 295, y = 50)
suffix_text.place(x = 435, y = 50)
addr1_text.place(x = 15, y = 100)
addr2_text.place(x = 15, y = 150)
city_text.place(x = 15, y = 200)
state_text.place(x = 318, y = 200)
zip_text.place(x = 367, y = 200)
# Defining and formatting Variables(2) for the data entered INTO the Entry boxes:
fname = StringVar()
mname = StringVar()
lname = StringVar()
suffix = StringVar()
addr1 = StringVar()
addr2 = StringVar()
city = StringVar()
state = StringVar()
zipcode = StringVar()
# Defining the Variables(3) for the Entry boxes:
fname_entry = Entry(newWindow, textvariable = fname, width = 14)
mname_entry = Entry(newWindow, textvariable = mname, width = 14)
lname_entry = Entry(newWindow, textvariable = lname, width = 14)
suffix_entry = Entry(newWindow, textvariable = suffix, width = 5)
addr1_entry = Entry(newWindow, textvariable = addr1, width = 51)
addr2_entry = Entry(newWindow, textvariable = addr2, width = 51)
city_entry = Entry(newWindow, textvariable = city, width = 32)
state_entry = Entry(newWindow, textvariable = state, width = 3)
zipcode_entry = Entry(newWindow, textvariable = zipcode, width = 13)
`enter code here`# Placing the Entry boxes:
fname_entry.place(x=15, y = 69)
mname_entry.place(x=155, y = 69)
lname_entry.place(x=295, y = 69)
suffix_entry.place(x=435, y = 69)
addr1_entry.place(x=15, y = 119)
addr2_entry.place(x=15, y = 169)
city_entry.place(x=15, y = 219)
state_entry.place(x=318, y = 219)
zipcode_entry.place(x=360, y = 219)
# Creates a Button and assigns it to the Variable: register. It is a gray, borderless button that when clicked, calls the saveInfo function.
register = Button(text = "REGISTER", bordercolor = 'gray', padx = 10, pady = 5, background='#BDBDBD', borderless = 'true', relief = 'groove', command = saveInfo)
# Places the Button
register.place (in_=newWindow, x = 200, y = 275)
newWindow.mainloop()
# End of createWindow function
style1 = ttk.Style()
style2 = ttk.Style()
style3 = ttk.Style()
style1.configure('topFrame.TFrame', background='green', borderwidth=4, relief='raised')
style2.configure('bottomFrame.TFrame', background = 'blue', borderwidth=6, relief = 'raised')
style3.configure('SrchLabel.TLabel')
eFrame = ttk.Frame(mainWindow, width=1060, height=200, style='topFrame.TFrame').grid()
rFrame = ttk.Frame(mainWindow, width=1060, height=600, style='bottomFrame.TFrame').grid()
# eFrame: Title text shown before the search terms entry box
srchFrame = ttk.Label(mainWindow, style = 'SrchLabel.TLabel', text = 'Patron Search')
srchFrame.place (x='30', y='40')
# eFrame: Search terms entry box
srchVar = StringVar()
srchEntry = tk.Entry(eFrame, bd = '1', width = '35')
srchEntry.place (x='130', y='37')
# eFrame: Button to execute Search
srchButton = Button(eFrame, text = 'Search', height = 30, width = 60, bd = '1')
srchButton.place(x = '470', y = '35')
# eFrame: Button to create New Record
newButton = Button(eFrame, text = 'New', height = 30, width = 60, bd = '1', command = createWindow)
newButton.place(x = '550', y = '35')
# rFrame: TAB Window Frame = from ttk Notebook
tabControl = ttk.Notebook(rFrame, width = 995, height = 528, style = 'rTabs.TNotebook')
# rFrame: TAB Elements
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab1=Frame(tabControl, background="#ffffff")
tab1.pack()
tab2=Frame(tabControl, background="#ffffff")
tab2.pack()
tab3=Frame(tabControl, background="#ffffff")
tab3.pack()
# rFrame: TAB Titles and Window placement
tabControl.add(tab1, text ='Patron')
tabControl.add(tab2, text ='Orders')
tabControl.add(tab3, text = 'Donations')
tabControl.place(x='5', y='205')
# TAB 1 Content
ttk.Label(tab1, text ='Patron Data').grid(column = 0, row = 0, padx = 30, pady = 30)
# TAB 2 Content
ttk.Label(tab2, text ='Order History and Info').grid(column = 0, row = 0, padx = 30, pady = 30)
# TAB 3 Content
ttk.Label(tab3, text ='Donation Info').grid(column = 0, row = 0, padx = 30, pady = 30)
# mainWindow: MENUBAR
menubar = Menu(mainWindow)
# mainWindow: Filemenu
file = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = mainWindow.destroy)
# mainWindow: Editmenu
edit = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
# mainWindow: Helpmenu
help_ = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
mainWindow.config(menu = menubar)
mainWindow.mainloop()
Again, I am not looking for advice on how to write better code, as I am just learning (2 weeks in). However any help you can provide in letting me know what I am doing wrong and where I can find an answer would be greatly appreciated!
Thanks in advance!

How do I proceed with the rest of the function to keep updating my message box in tkinter?

I am trying to do a message box to warns the user in my UI that they will be moved to the homepage after 10 seconds, codes are as following:
from tkinter import *
import warnings
import random
import time
import sqlite3
from tkinter import simpledialog
from tkinter import messagebox
from tkcalendar import *
from tkinter import ttk
import math
from PIL import Image, ImageTk
import winsound
##-------------Frames setup--------------------------
class VendingApp(Tk):
def __init__(self):
Tk.__init__(self)
self._frame = None
self.switch_frame(Home)
def switch_frame(self, frame_class):
#Destroys current frame and replaces it with a new one.
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
####-----------------------Home page---------------------------
class Home(Frame):
def __init__(self, master):
Frame.__init__(self, master)
##-----------------------fuctions-----------------------------------
def clicked(a):
if (a.x <=1920) and (a.y<=1080):
master.switch_frame(Store)
print ("1")
else:
None
None
##----------------setup------------------------------------------
self._images = list()
img_banner = Image.open("pic/banner.jpg")
img_banner = img_banner.resize((400,100), Image.ANTIALIAS)
banner = ImageTk.PhotoImage(img_banner)
self._images.append(banner)
##---------------------Top frame Home------------------------------------
topFrame = Frame(self,width = 1920, height = 1080)
topFrame.pack()
canvasM = Canvas(topFrame,height=1080, width=1920)
canvasM['highlightthickness'] = 0
canvasM.pack()
body = canvasM.create_rectangle(0, 0,1920,1080, fill = 'gray95')
mylabel = canvasM.create_text((960, 390),font=("Purisa", 40), text="Touch anywhere to continue")
canvasM.tag_bind(body,"<Button>",clicked)
canvasM.tag_bind(mylabel,"<Button>",clicked)
#-------------------------Store page--------------------------------------------------------------
class Store(Frame):
def __init__(self, master):
Frame.__init__(self, master)
def timeset():
global time
time = 15
def active(event):
global time
time = 15
print (time)
def timeout():
global time
if time >= 10:
time -= 1
master.after(1000, timeout)
elif time <= 10 and time > 0:
askuser()
time -= 1
master.after(1000, timeout)
elif time <= 0:
master.switch_frame(Home)
print(time)
def askuser():
ask = messagebox.showinfo('Are you there?','Returning to home page in ' + (str(time)) + ' seconds. \n please touch the screen to continue using the app')
self._images = list()
img_banner = Image.open("pic/banner.jpg")
img_banner = img_banner.resize((400, 100), Image.ANTIALIAS)
banner = ImageTk.PhotoImage(img_banner)
self._images.append(banner)
##---------------------pictures and filters Store------------------------------------
PicFrame = Frame(self, width=400, height=100)
PicFrame.grid(row = 0, column = 0)
PicFrame.grid_propagate(False)
canvas_for_banner = Canvas(PicFrame, height=100, width=400 ) # banner image
canvas_for_banner.pack(anchor = N)
canvas_for_banner['highlightthickness'] = 0
canvas_for_banner.grid_propagate(False)
canvas_for_banner.create_image(0, 0, anchor=NW, image=banner)
##---------------------------------Midd store-------------------
FilterFrame =Frame(self, width=400, height=930, relief = RAISED, bd =1)
FilterFrame.grid(row = 1, column = 0)
FilterFrame.pack_propagate(0)
Ava_title = Label(FilterFrame, text = "Availability", font = ('Helvetica', 20, 'bold'))
Ava_title.pack(pady = (100,0))
self.stock_yes = IntVar()
self.stock_no = IntVar()
check_ava = Checkbutton(FilterFrame, text = 'Avaliable', variable = self.stock_yes, font = 20, command = None)
check_ava.pack( anchor = 'w', padx = 30, pady =10)
check_unava = Checkbutton(FilterFrame, text = 'Unavaliable', font = 20, variable = self.stock_no, command = None)
check_unava.pack(anchor = 'w', padx = 30, pady = 10)
gend_title = Label(FilterFrame, text = "Gender", font = ('Helvetica', 20, 'bold'))
gend_title.pack(pady = (30,0))
self.boi = IntVar()
self.girl = IntVar()
check_boi = Checkbutton(FilterFrame, text = 'Male', font = 20, variable = self.boi)
check_boi.pack( anchor = 'w', padx = 30, pady = 10)
cehck_girl = Checkbutton(FilterFrame, text = 'Female', font = 20, variable = self.girl)
cehck_girl.pack(anchor = 'w', padx = 30, pady =10)
Class_title = Label(FilterFrame, text = "Uniform class", font = ('Helvetica', 20, 'bold'))
Class_title.pack(pady = (30,0))
self.formal = IntVar()
self.sport = IntVar()
check_formal = Checkbutton(FilterFrame, text = 'Formal', font = 20, variable = self.formal)
check_formal.pack( anchor = 'w', padx = 30, pady =10)
check_sport = Checkbutton(FilterFrame, text = 'Sport', font = 20, variable = self.sport)
check_sport.pack(anchor = 'w', padx = 30, pady =10)
type_title = Label(FilterFrame, text = "Type", font = ('Helvetica', 20, 'bold'))
type_title.pack(pady = (30,0))
self.shirts = IntVar()
self.pants = IntVar()
self.misc = IntVar()
check_shirts = Checkbutton(FilterFrame, text = 'Shirts', font = 20, variable = self.shirts)
check_shirts.pack( anchor = 'w', padx = 30, pady =10)
check_pants = Checkbutton(FilterFrame, text = 'Pants', font = 20, variable = self.pants)
check_pants.pack(anchor = 'w', padx = 30, pady =10)
check_misc = Checkbutton(FilterFrame, text = 'Misc', font = 20, variable = self.misc)
check_misc.pack(anchor = 'w', padx = 30, pady =10)
##------------------------\\\\\\\\\\\\\\\\\\\\\\---------------------------------------------
MidFrame = Frame(self,width = 1520, height = 1030, relief = SUNKEN, bd = 2)
MidFrame.grid(row = 0, column = 1, rowspan = 2)
MidFrame.grid_propagate(False)
store_canvas = Canvas(MidFrame, width = 1520, height = 1030)
store_canvas.pack()
store_canvas.pack_propagate(0)
frames = []
frame_order = []
num = 1
for x in range(4):
frames.append([])
for y in range(4):
frames[x].append(0)
for x in range(4):
for y in range(4):
frames[x][y] = Frame(store_canvas, width=1520 / 4, height=1030 / 4, bd = 2, relief = SOLID)
frames[x][y].grid(row=y, column=x)
frames[x][y].pack_propagate(False)
frame_order.append(frames[x][y])
for frame in frame_order:
Label(frame, text=num, anchor='nw').pack( side = 'left')
num += 1
##------------------------\\\\\\\\\\\\\\\\\\\\\\---------------------------------------------
BottomFrame = Frame(self,width = 1920, height = 50, bd = 2, relief = RAISED)
BottomFrame.grid(row = 2, column =0, columnspan = 2)
BottomFrame.pack_propagate(False)
help_btn = Button(BottomFrame, width = 5, height = 3, text = '?', image = None)
help_btn.pack(side = 'right')
master.bind("<Button>",active)
timeset()
timeout()
#----------------------------------------------------------------------------------------
if __name__ == "__main__":
root = VendingApp()
#Renames the TITLE of the window
root.title("Vending machine")
root.geometry("1920x1080")
root.attributes('-fullscreen', True)
root.resizable(False, False)
root.mainloop()
The problem is that the whole program 'freezes' every time the msg box pop up and unless the user confirms 'ok' then the function will pick up where it left off. Is there any way to keep the function going, hence the number in the msg box will update according to the time remaining? Am I approaching this problem the wrong way? Is there another module for this all along and I am just using the wrong module for the task? Please go easy on me, I am still learning. All responses all much appreciated.
messagebox will pause the further execution until it receives an input. To prevent this you can try the following
Use threading
from tkinter import *
from tkinter import messagebox
from threading import Thread
def msgbox():
def _display():
messagebox.showinfo('Info','Self distruction after 2 seconds')
Thread(target=_display).start()
root.after(2000,root.destroy)
root=Tk()
button=Button(root,text='Run',command=msgbox)
button.pack()
root.mainloop()
Create your own info box using Toplevel (the below example will create a replica (sort of) of standard windows info box)
from tkinter import *
import tkinter.ttk as ttk
class InfoBox(Toplevel):
def __init__(self,title,message,parent=None):
Toplevel.__init__(self,parent)
self.bell()
self.transient(self.master)
self.title(title)
self.config(bg='white')
top_frame=Frame(self,bd=0,bg=self['bg'])
top_frame.pack(side='top',fill='x',pady=20)
bottom_frame=Frame(self,bd=0)
bottom_frame.pack(side='bottom',fill='x')
self.info_icon=Canvas(top_frame,width=36,height=36
,bg=self['bg'],bd=0,highlightthickness=0)
self.info_icon.create_oval(0,0,30,30,fill='#0077be',outline='#0077be')
self.info_icon.create_text(15,16,text='i',font=('',18),fill='white')
self.info_icon.pack(side='left',padx=(20,0),anchor='center')
self.label=Label(top_frame,text=message,bg=self['bg'])
self.label.pack(padx=(1,20),pady=5,anchor='center')
self.ok_button=ttk.Button(bottom_frame,text='OK',
state='active',command=self.destroy)
self.ok_button.pack(anchor='e',padx=15,pady=10)
self.update()
center_x=self.winfo_screenwidth()//2-self.winfo_width()//2
center_y=self.winfo_screenheight()//2-self.winfo_height()//2
self.geometry(f'+{center_x}+{center_y}')
def msgbox():
InfoBox('Info','Self distruction after 2 seconds')
root.after(2000,root.destroy)
root=Tk()
button=Button(root,text='Run',command=msgbox)
button.pack()
root.mainloop()
UPDATE
Stacking is automatically prevented
from tkinter import *
import tkinter.ttk as ttk
class InfoBox(Toplevel):
def __init__(self):
self.exists=False
def call(self,title,message,parent=None):
if self.exists:
self._destroy()
Toplevel.__init__(self,parent)
self.bell()
self.transient(self.master)
self.title(title)
self.config(bg='white')
top_frame=Frame(self,bd=0,bg=self['bg'])
top_frame.pack(side='top',fill='x',pady=20)
bottom_frame=Frame(self,bd=0)
bottom_frame.pack(side='bottom',fill='x')
self.info_icon=Canvas(top_frame,width=36,height=36
,bg=self['bg'],bd=0,highlightthickness=0)
self.info_icon.create_oval(0,0,30,30,fill='#0077be',outline='#0077be')
self.info_icon.create_text(15,16,text='i',font=('',18),fill='white')
self.info_icon.pack(side='left',padx=(20,0),anchor='center')
self.label=Label(top_frame,text=message,bg=self['bg'])
self.label.pack(padx=(1,20),pady=5,anchor='center')
self.ok_button=ttk.Button(bottom_frame,text='OK',
state='active',command=self._destroy)
self.ok_button.pack(anchor='e',padx=15,pady=10)
self.update()
center_x=self.winfo_screenwidth()//2-self.winfo_width()//2
center_y=self.winfo_screenheight()//2-self.winfo_height()//2
self.geometry(f'+{center_x}+{center_y}')
self.protocol('WM_DELETE_WINDOW',self._destroy)
self.exists=True
def _destroy(self):
self.destroy()
self.exists=False
root=Tk()
infobox=InfoBox()
button=Button(root,text='Hello',command=lambda:infobox.call('Info','Hello'))
button.pack()
button1=Button(root,text='World',command=lambda:infobox.call('Info','World'))
button1.pack()
root.mainloop()

Why does my python program cut off a good section of my tkinter output?

I'm programming a simple application to help with tracking combat information for dnd using python and tkinter. I started with the input page and it worked fine, but when I started to use tabs to allow the user to access a help page, strangely it cut off about half of the original output, the only thing I changed is using ttk and my_frame 1 instead of root. I've tried changing .place to .grid and it worked to some degree, although instead it displayed the buttons in strange positions in front of the rest of the outputs. Then I tried adding blank lines on the help tag, this did work but it seems quite ineffective and time consuming. I can guess from this there's no space but the geometry was set to 500x500 which should be plenty. So I'm trying to figure out what's going wrong. Thanks for reading!
(Also it isn't quite complete yet hence why the next page function does something random)
This is the code:
import tkinter as tk
from tkinter import ttk
#Starting up tkinter
root = tk.Tk()
root.title("Dungeons and dragons combat manager")
root.geometry("500x500")
root.configure(background = "White")
#Initialise the arrays
player_details = []
overall_data = []
#help page
my_notebook = ttk.Notebook(root)
my_notebook.pack(pady = 15)
my_frame1 = tk.Frame(my_notebook, width = "500", height = "500")
my_frame1.grid(row = 0, column = 0, columnspan = 2, padx = 40, pady = 5)
my_frame2 = tk.Frame(my_notebook, width = 500, height = 500)
my_notebook.add(my_frame1, text = "Main")
my_notebook.add(my_frame2, text = "Help")
line1 = tk.Label(my_frame2, text = "Hello, welcome to the help page!", font = ('Times New Roman', 12)).grid(row = 0, column = 0, sticky = 'W')
line2 = tk.Label(my_frame2, text = "Most of the instructions will be given during application use", font = ('Times New Roman', 12)).grid(row = 1, column = 0, sticky = 'W')
line3 = tk.Label(my_frame2, text = "Please ensure all numerical inputs are given in integers", font = ('Times New Roman', 12)).grid(row = 2, column = 0, sticky = 'W')
line4 = tk.Label(my_frame2, text = "If any mistakes are made please press the restart button", font = ('Times New Roman', 12)).grid(row = 3, column = 0, sticky = 'W')
line5 = tk.Label(my_frame2, text = "Once the order is displayed initiative cannot be changed", font = ('Times New Roman', 12)).grid(row = 4, column = 0, sticky = 'W')
line6 = tk.Label(my_frame2, text = "However, health can be edited at any time", font = ('Times New Roman', 12)).grid(row = 5, column = 0, sticky = 'W')
line7 = tk.Label(my_frame2, text = "Good luck with your encounter!", font = ('Times New Roman', 12)).grid(row = 6, column = 0, sticky = 'W')
#main section
tk.Label(my_frame1, text = "Player details form", font = ('Times New Roman', 16)).grid(row = 0, column = 0, padx = 25, pady = 5)
#function definitions
def submit():
player_details = []
player_details.append(name.get())
player_details.append(health.get())
player_details.append(ac.get())
player_details.append(initiative.get())
overall_data.append(player_details)
print(str(overall_data))
player_num = tk.Label(root, text = "Inputting for Player :" + str(len(overall_data)+1), fg = 'White', bg = "Black")
player_num.place(x = 30, y = 210, width = 240, height = 25)
def clear():
name.delete(0, 'end')
health.delete(0, 'end')
ac.delete(0, 'end')
initiative.delete(0, 'end')
name.focus_set()
def next_page():
print("chicken")
#display
player_data = ['Name: ','Health: ','Armour Class: ','initiative: ']
labels = range(4)
for i in range(4):
bg_colour = "Black"
l = tk.Label(my_frame1,
text = player_data[i],
fg='White',
bg=bg_colour)
l.place(x = 20, y = 60 + i*30, width=120, height=25)
name = tk.Entry(my_frame1,width = 15,fg = "Black",bg = "White")
name.place(x = 150, y = 60, width = 120, height=25)
health = tk.Entry(my_frame1, width = 15,fg = "Black",bg = "White")
health.place(x = 150, y = 90, width = 120, height=25)
ac = tk.Entry(my_frame1, width = 15,fg = "Black",bg = "White")
ac.place(x = 150, y = 120, width = 120, height=25)
initiative = tk.Entry(my_frame1, width = 15,fg = "Black",bg = "White")
initiative.place(x = 150, y = 150, width = 120, height=25)
submit_button = tk.Button(my_frame1, text = "Submit", command = submit)
submit_button.place(x = 30, y = 180, width = 100, height = 25)
clear_button = tk.Button(my_frame1, text = "Clear", command = clear)
clear_button.place(x = 160, y = 180, width = 100, height = 25)
player_num = tk.Label(my_frame1, text = "Inputting for Player : 1", fg = 'White', bg = "Black")
player_num.place(x = 30, y = 210, width = 240, height = 25)
next_button = tk.Button(my_frame1, text = "Next", command = next_page)
next_button.place(x = 30, y = 240, width = 100, height = 25)
'''
It is because you haven't made the notebook large enough, and haven't requested that it expand to fill the available space. You're relying on the default size of the notebook, and you're putting more things in than will fit. Plus, you're using place instead of pack inside the notebook, and place won't cause the containing window to grow or shrink. This is one of the disadvantages to using place as a general purpose layout mechanism.
The easy solution is to use the options that pack supports, such as fill and expand, though without knowing what your full UI should look like it's hard to say if that's the correct solution or not.
my_notebook.pack(fill="both", expand=True)
Another solution would be to stop using place. If you use grid and/or pack, then the notebook will resize to fit its contents.

Categories

Resources