简体   繁体   English

Python3:TkInter

[英]Python3: TkInter

I've just coded this simple program but need help with Tkinter! 我刚刚编写了这个简单的程序,但是需要Tkinter的帮助! I want to use what the user types into the adult ticket box so i set it as global but because the user hasn't clicked the button yet the input_adult.get() only returns a blank string instead of the integer the user typed. 我想使用用户在成人票框中输入的内容,因此将其设置为全局票,但是由于用户尚未单击按钮,而input_adult.get()仅返回空字符串,而不是用户键入的整数。 Is there any way to get around this? 有什么办法可以解决这个问题? Thanks in advance!! 提前致谢!!

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()

You need to call the get method in the callback for the button. 您需要在按钮的回调中调用get方法。 This requires that you make the entry widget available globally: 这要求您使条目窗口小部件在全局范围内可用:

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

def adult_travel():
    adult_tickets = input_adult.get()
    print(adult_tickets)

I would do it like this. 我会这样做。 (There are some things here you didn't ask for, but I use as part of my boilerplate since they are pretty useful. Things like allowing [RETURN] key to work without focusing on button, etc). (这里没有您不需要的某些内容,但由于它们非常有用,因此我将其用作样板的一部分。例如使[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())

I have modified your code slightly: 我已经稍微修改了您的代码:

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

I commented the alterations. 我评论了改动。 With me it prints the number (actually the string). 和我一起打印数字(实际上是字符串)。

My first question is what version is your Python, 2X or 3X? 我的第一个问题是您的Python,2X或3X是哪个版本? if you're running Python 3X then you have to import tkinkter like this if you want to have access to Tk this is how you do it 如果您正在运行Python 3X,则必须像这样导入tkinkter,如果您想访问Tk,这就是您的操作方式

import tkinter as tk

class YourClassName(tk.Tk))

If you're running Python 2X then you have to import tkinter like this, 如果您使用的是Python 2X,则必须像这样导入tkinter,

import tkinter as Tk

I would try changing the import and see what happens. 我会尝试更改导入,然后看看会发生什么。 If this does not work I would like to see the error message's you get. 如果这不起作用,我希望看到错误消息。 Hope this helps. 希望这可以帮助。

The Entry widget must have a variable in the 'textvariable' parameter , the variable type should already be declared . Entry小部件的'textvariable'参数中必须有一个变量,该变量类型应已声明。 Like consider adding the following 3 step process in ur code ; 就像考虑在您的代码中添加以下三步过程;

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 .

Always the Entry widget should contain a variable & it should already be assingned as either IntVar()/StringVar() before using it as a 'textvariable' parameter inside the Entry widget . 始终,Entry小部件应包含一个变量,并且在将其用作Entry小部件内的“ textvariable”参数之前,应将其已初始化为IntVar()/ StringVar()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM