我正在尝试实现一个非常简单的交互式tkinter事物,用户通过按下按钮来输入钱(支付5英镑的票)。它本身可以正常工作(即下面的代码),但是当我尝试将它放在一个可以在窗口之间导航的tkinter程序时它不起作用。运行这个以查看交互式货币输入内容的含义:
from tkinter import *
# here I have used the pack() method, but in the later code example I use grid
# so that my code is compatible with the proposed code I copied from the solution (see below)
def MenuScreen(): # screen
global tkinter
root = Tk()
frame = Frame(root)
frame.pack()
root.geometry("300x100")
welcome = Label(frame, text="Welcome to the Durahm gardens. Enter an option:")
welcome.pack(side="top")
PurchaseButton = Button(frame,text='Buy a ticket', command=SideScreen1)
PurchaseButton.pack(side='bottom')
ManagerButton = Button(frame,text='Change ticket price', command=SideScreen2)
ManagerButton.pack(side='bottom')
root.mainloop()
TicketPrice = 5
NumEntered = 0
def SideScreen1(): # purchase ticket
def PoundEntry():
NumEntered += 1
global TicketPrice, notice, NumEntered
print(NumEntered, TicketPrice)
if NumEntered == TicketPrice:
root.notice.config(text="Money entered: "+str(NumEntered))
root.destroy()
else:
root.notice.config(text="Money entered: "+str(NumEntered))
root = Tk()
root.explanation = Label(root, text="~Enter 1 pound at a time~", bg="aquamarine",\
height=3,width=20, font=(None, 10))
root.explanation.pack(side=TOP, fill=BOTH)
root.notice = Label(root, text="Money entered: 0", bg="aquamarine",\
font=(None, 16),height=8,width=30) # font=(None, 15)
root.notice.pack(side=BOTTOM, fill=BOTH)
root.PoundButton = Button(root, text="Enter £1",
font=(None, 16), height=3, width=13,\
command=PoundEntry)
root.PoundButton.pack(fill=BOTH)
root.mainloop()
def SideScreen2():
## the manager changes the password
print("Manager")
MenuScreen()
到目前为止,我已经尝试使用交互式tkinter来实现解决导航问题的this navigation solution代码,但这两个东西似乎不相容。
这是这段代码。我留下了关于原始“导航解决方案”代码的评论(您可以在上面的链接中看到),包括我自己的评论,其中我试图添加我的交互式英镑输入内容。 我现在得到的错误是'NameError:name'notice'未定义'但我不完全明白在没有发生这种情况的情况下我可以定义'notice':
from tkinter import *
def create_widgets_in_first_frame(): # try
# Create the label for the frame
welcome = Label(first_frame, text='(Window 1)Welcome to the Durahm gardens. Enter an option:')
welcome.grid(column=0, row=0, pady=10, padx=10, sticky=N)
# Create the button for the frame
PurchaseButton = Button(first_frame,text='Buy a ticket', command=call_second_frame_on_top) # quit
PurchaseButton.grid(column=0, row=1, pady=10, sticky=N)
first_window_next_button = Button(first_frame, text = "Change ticket price", command = SideScreen1)
first_window_next_button.grid(column=1, row=1, pady=10, sticky=N)
def create_widgets_in_second_frame():
# Create the label for the frame
second_window_label = Label(second_frame, text='Window 2')
second_window_label.grid(column=0, row=0, pady=10, padx=10, sticky=(N))
TicketPrice = 5 # defining the variables
NumEntered = 0
# purhcasing the ticket
def PoundEntry():
NumEntered += 1
global TicketPrice, notice, NumEntered
print(NumEntered, TicketPrice)
if NumEntered == TicketPrice:
notice.config(text="Money entered: "+str(NumEntered))
print("Passsing")
else:
notice.config(text="Money entered: "+str(NumEntered))
explanation = Label(second_frame, text="(Enter 1 pound at a time)")
explanation.grid(column=1, row=0)
notice = Label(second_frame, text="Money entered: 0")
notice.grid(column=1, row=1)
PoundButton = Button(second_frame, text="Enter £1", command=PoundEntry)
PoundButton.grid(column=0, row=1)
second_window_back_button = Button(second_frame, text = "Back", command = call_first_frame_on_top)
second_window_back_button.grid(column=0, row=2, pady=10, sticky=(N))
second_window_next_button = Button(second_frame, text = "Next", command = call_third_frame_on_top)
second_window_next_button.grid(column=1, row=2, pady=10, sticky=(N))
def create_widgets_in_third_frame():
# Create the label for the frame
third_window_label = Label(third_frame, text='Window 3')
third_window_label.grid(column=0, row=0, pady=10, padx=10, sticky=(N))
# Create the button for the frame
third_window_back_button = Button(third_frame, text = "Back", command = call_second_frame_on_top)
third_window_back_button.grid(column=0, row=1, pady=10, sticky=(N))
third_window_quit_button = Button(third_frame, text = "Quit", command = SideScreen1)
third_window_quit_button.grid(column=1, row=1, pady=10, sticky=(N))
def call_first_frame_on_top():
# This function can be called only from the second window.
# Hide the second window and show the first window.
second_frame.grid_forget()
first_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(W, N, E))
def call_second_frame_on_top():
# This function can be called from the first and third windows.
# Hide the first and third windows and show the second window.
first_frame.grid_forget()
third_frame.grid_forget()
second_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(W, N, E))
def call_third_frame_on_top():
# This function can only be called from the second window.
# Hide the second window and show the third window.
second_frame.grid_forget()
third_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(W, N, E))
def SideScreen1():
pass # the manager changes the password
###############################
# Main program starts here #
###############################
# Create the root GUI window.
root_window = Tk()
# Define window size
window_width = 200
window_heigth = 100
# Create frames inside the root window to hold other GUI elements. All frames must be created in the main program, otherwise they are not accessible in functions.
first_frame=Frame(root_window, width=window_width, height=window_heigth)
first_frame['borderwidth'] = 2
first_frame['relief'] = 'sunken'
first_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(W, N, E))
second_frame=Frame(root_window, width=window_width, height=window_heigth)
second_frame['borderwidth'] = 2
second_frame['relief'] = 'sunken'
second_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(W, N, E))
third_frame=Frame(root_window, width=window_width, height=window_heigth)
third_frame['borderwidth'] = 2
third_frame['relief'] = 'sunken'
third_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(W, N, E))
# Create all widgets to all frames
create_widgets_in_third_frame()
create_widgets_in_second_frame()
create_widgets_in_first_frame()
# Hide all frames in reverse order, but leave first frame visible (unhidden).
third_frame.grid_forget()
second_frame.grid_forget()
我正在尝试结合两件事:首先,有一个类似向导的程序,向用户显示一系列窗口,用户可以在窗口之间单击下一步和后退按钮。其次,我想把简单的交互式tkinter东西(输入钱)放到其中一个窗口上,这样用户可以点击一个按钮,然后窗口就会改变,这样他们就可以买票了。
我认为一种完全不同的方法可以让我更轻松地在导航程序中实现票务付款。上面的代码(reminder link)可能对我正在尝试做的事情毫无意义。这个导航程序绝对理想的是以下方法:
但是通过这样做,这似乎使得无法摆脱在后台挥之不去的根窗口。有没有人有任何建议?