0

我刚刚编写了这个简单的程序,但需要 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()
4

5 回答 5

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

我评论了这些改动。和我一起打印数字(实际上是字符串)。

于 2015-03-28T16:46:04.543 回答
0

您需要get在按钮的回调中调用该方法。这要求您使条目小部件全局可用:

def adult_Gui():
    global input_adult
    ...
    input_adult = Entry()
    ...

def adult_travel():
    adult_tickets = input_adult.get()
    print(adult_tickets)
于 2013-09-24T10:59:39.723 回答
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())
于 2013-09-24T18:10:16.543 回答
0

我的第一个问题是你的 Python 是什么版本,2X 还是 3X?如果您正在运行 Python 3X,那么您必须像这样导入 tkinkter 如果您想访问 Tk,您就是这样做的

import tkinter as tk

class YourClassName(tk.Tk))

如果您正在运行 Python 2X,那么您必须像这样导入 tkinter,

import tkinter as Tk

我会尝试更改导入,看看会发生什么。如果这不起作用,我希望看到您收到的错误消息。希望这可以帮助。

于 2017-07-23T20:28:23.353 回答
-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 小部件始终应该包含一个变量,并且在将其用作 Entry 小部件内的“textvariable”参数之前,它应该已经被分配为 IntVar()/StringVar()。

于 2013-09-24T07:14:08.170 回答