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

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

私はあなたのコードを少し修正しました:

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 に答える