我刚编写了这个简单的程序,但需要Tkinter的帮助!我想使用用户在成人票箱中键入的内容,因此我将其设置为全局,但由于用户尚未单击按钮,因此input_adult.get()仅返回空白字符串而不是用户键入的整数。有没有办法解决这个问题?在此先感谢!!
from tkinter import *
import sys
adult_fare = 7.45
child_fare = 5.00
adult_tickets = 0
def child_Gui():
mGui = Tk()
labelNo = Label(mGui, text = "How many child/concession tickets do you need?").pack()
input_child = Entry(mGui)
input_child.pack()
input_child.focus_set()
b = Button(mGui, text = "GO", width = 10, command = child_travel)
b.pack()
def adult_travel():
print(adult_tickets)
def adult_Gui():
global adult_tickets
mGui = Tk()
labelNo = Label(mGui, text = "How many adult tickets do you need?").pack()
input_adult = Entry(mGui)
input_adult.pack()
input_adult.focus_set()
b = Button(mGui, text = "GO", width = 10, command = adult_travel)
b.pack()
adult_tickets = input_adult.get()
def compare_sunday():
sunday_answer = sundayEntry.get().lower()
if sunday_answer == "yes":
global child_fare
global adult_fare
adult_fare = 3.00
child_fare = 3.00
labelNo = Label(sundayGui, text = "Ok your traveling on a sunday. All prices will be $3.00!!").pack()
okButton = Button(sundayGui, text = "Click here to continue", width = 40, command = adult_Gui).pack()
elif sunday_answer == "no":
labelNo = Label(sundayGui, text = "Ok your not traveling on a sunday.").pack()
okButton = Button(sundayGui, text = "Click here to continue", width = 40, command = adult_Gui).pack()
else:
labelElse = Label(sundayGui, text = "Please type yes or no!!").pack()
sundayGui = Tk()
sundayGui.title("Travel Calculator")
label_sunday = Label(sundayGui, text = "Are you traveling on a sunday?").pack()
sundayEntry = Entry(sundayGui)
sundayEntry.pack()
sundayEntry.focus_set()
sundayB = Button(sundayGui, text = "Go", width = 10, command = compare_sunday).pack()
答案 0 :(得分:0)
您需要在回调按钮中调用get
方法。这要求您在全局范围内使用条目小部件:
def adult_Gui():
global input_adult
...
input_adult = Entry()
...
def adult_travel():
adult_tickets = input_adult.get()
print(adult_tickets)
答案 1 :(得分:0)
我会这样做。 (这里有一些你没有要求的东西,但是我用它作为我的样板文件的一部分,因为它们非常有用。比如允许[RETURN]键工作而不关注按钮等)。
import tkinter
import tkMessageBox
def get_user_input():
popup = UserInput()
if popup.command():
return popup.command()
else:
message = 'Nothing was entered'
tkMessageBox.showerror('Error', message)
class UserInput:
def __init__(self):
self.master = tkinter.Tk()
l_text = 'Operator input here'
self.label = Tkinter.Label(self.master, text=l_text)
self.label.pack()
self.textbox = tkinter.Entry(self.master, takefocus=1)
self.textbox.pack()
self.button = tkinter.Button(
self.master,
text="OK",
command=self.command,
default=tkinter.ACTIVE)
self.button.pack()
self.master.bind('<Shift-Return>', self.command)
self.textbox.focus_set()
self.master.mainloop()
def command(self, event=None):
user_input = self.textbox.get()
if user_input == '':
title = 'Input'
message = 'Some input must be entered'
tkMessageBox.showerror(title, message)
else:
self.master.withdraw()
self.master.quit()
return user_input
print (get_user_input())
答案 2 :(得分:0)
我稍微修改了你的代码:
from tkinter import *
import sys
adult_fare = 7.45
child_fare = 5.00
adult_tickets = 0
def child_Gui():
mGui = Tk()
labelNo = Label(mGui, text = "How many child/concession tickets do you need?").pack()
input_child = Entry(mGui)
input_child.pack()
input_child.focus_set()
b = Button(mGui, text = "GO", width = 10, command = child_travel)
b.pack()
def adult_travel():
global adult_tickets # added
global input_adult # added
adult_tickets = input_adult.get() # moved from def adult_Gui()
print(adult_tickets)
def adult_Gui():
global adult_tickets
global input_adult # added
mGui = Tk()
labelNo = Label(mGui, text = "How many adult tickets do you need?").pack()
input_adult = Entry(mGui)
input_adult.pack()
input_adult.focus_set()
b = Button(mGui, text = "GO", width = 10, command = adult_travel)
b.pack()
def compare_sunday():
sunday_answer = sundayEntry.get().lower()
if sunday_answer == "yes":
global child_fare
global adult_fare
adult_fare = 3.00
child_fare = 3.00
labelNo = Label(sundayGui, text = "Ok your traveling on a sunday. All prices will be $3.00!!").pack()
okButton = Button(sundayGui, text = "Click here to continue", width = 40, command = adult_Gui).pack()
elif sunday_answer == "no":
labelNo = Label(sundayGui, text = "Ok your not traveling on a sunday.").pack()
okButton = Button(sundayGui, text = "Click here to continue", width = 40, command = adult_Gui).pack()
else:
labelElse = Label(sundayGui, text = "Please type yes or no!!").pack()
sundayGui = Tk()
sundayGui.title("Travel Calculator")
label_sunday = Label(sundayGui, text = "Are you traveling on a sunday?").pack()
sundayEntry = Entry(sundayGui)
sundayEntry.pack()
sundayEntry.focus_set()
sundayB = Button(sundayGui, text = "Go", width = 10, command = compare_sunday).pack()
sundayGui.mainloop() # added
我评论了这些改动。和我一起打印数字(实际上是字符串)。
答案 3 :(得分:0)
我的第一个问题是你的Python,2X或3X是什么版本的?如果您正在运行Python 3X,那么您必须导入tkinkter,如果您想要访问Tk,这就是您的操作方式
import tkinter as tk
class YourClassName(tk.Tk))
如果你正在运行Python 2X,那么你必须像这样导入tkinter,
import tkinter as Tk
我会尝试更改导入,看看会发生什么。如果这不起作用,我希望看到您收到的错误消息。希望这会有所帮助。
答案 4 :(得分:-2)
Entry小部件必须在'textvariable'参数中有一个变量,应该已经声明了变量类型。请考虑在您的代码中添加以下3个步骤;
Var1=IntVar() # 1.First we declare the variable as IntVar()/StringVar() for intergers & strings respectively
input_child = Entry(mGui,textvariable=var1) # 2.Now we assign the variable to a 'textvariable' parameter in the Entry widget.
var1.get() # 3. Now we get the input stored in the variable var1 using the get method .
始终,Entry小部件应包含变量&amp;在将它作为Entry小部件中的'textvariable'参数使用之前,它应该已经被分配为IntVar()/ StringVar()。