tkinter按钮可以通过点击返回条目中的值吗?

时间:2015-01-20 01:15:34

标签: python-3.x tkinter return

我做了一个扩展项目,作为我目前大学的资格之一,我选择编写一个python战略/角色扮演游戏。结果,我最终获得了最高级别的Python知识(超越我的计算老师,他只使用过基础知识......并且几年前才使用过Tkinter。每个决定制作程序的人都是要么用Lua,Java,C ++,HTML / CSS / Java-Script编写代码,要么用python编写代码,他们只使用从老师那里学到的基础知识。) 我说"最高级别的Python知识"但实际上并不是那么高......我只知道一点点基础知识。 因此,论坛帖子是我可以寻求帮助的最佳地点。

所以在我的游戏中我定义了这个功能:

#"Given_String" is the question that one would want to ask. (With the answer being an integer between 1 and "Choice_Range" (inclusive)
def Value_Error(Given_String,Error_Message,Choice_Range):
    while True:
        try:
            Temp=int(input(Given_String))
            if Temp<1 or Temp>Choice_Range:
                print(Error_Message)
            else:
                break
        except ValueError:
            print(Error_Message)
    return Temp

然后我想将tkinter添加到我的代码中,因为游戏必须在一个单独的窗口中,而不是在控制台中。因此,我不得不更改此代码,以便显示&#34; Given_Message&#34;和#34; Error_Message&#34;在tkinter窗口中,并使用在定义&#34; Temp&#34;时输入到输入框中的值。

我编写了这段代码来完成这项工作:(或至少大部分内容)

#This code is stored in a different file for neatness and hence I had to import "sys" to avoid circular imports.
#This code is made to be flexible so that I can later re-use it when necessary.
#This code starts with the function all the way at the bottom. The rest are made to add flexibility and to structure the algorithm.
#This code hasn't been fully run (Because of the Error crashing the Python Shell) so it can contain other Run-time Errors that I'm not aware of yet.

import sys
def Generate_Window(Window_Name,X_Parameter=5,Y_Parameter=50):
    Temp=sys.modules['tkinter'].Tk()
    Temp.title(Window_Name)
    Temp.geometry(str(X_Parameter)+"x"+str(Y_Parameter))
    return Temp

def Generate_Button(Master,Text="Submit"):
    Temp=sys.modules["tkinter"].Button(Master,text=Text)
    return Temp

def Generate_Entry(Master):
    Temp=sys.modules["tkinter"].Entry(Master)
    return Temp

def Generate_Label(Master,Given_String):
    Temp=sys.modules["tkinter"].Label(Master,text=Given_String)
    return Temp

def Com_Get_Entry(Given_Window,Given_Entry):
    Temp=Given_Entry.get()
    Given_Window.destroy()
    return Temp

def Com_Confirm(Given_Window):
    Given_Window.destroy()

def Generate_Entry_Box(Given_String):
    Entry_Window=Generate_Window("Entry",X_Parameter=300)
    Entry_Label=Generate_Label(Entry_Window,Given_String)
    Entry_Entry=Generate_Entry(Entry_Window)
    Entry_Button=Generate_Button(Entry_Window)
    Entry_Button.configure(command=lambda:Com_Get_Entry(Entry_Window,Entry_Entry))
    Entry_Label.grid(row=0,columnspan=2)
    Entry_Entry.grid(row=1,column=0)
    Entry_Button.grid(row=1,column=1)

def Generate_Alert_Message(Given_String):
    Alert_Window=Generate_Window("Alert",X_Parameter=300)
    Alert_Label=Generate_Label(Alert_Window,Given_String)
    Alert_Button=Generate_Button(Alert_Window,Text="OK")
    Alert_Button.configure(command=lambda:Com_Confirm(Alert_Window))
    Alert_Label.grid(row=0,columnspan=2)
    Alert_Button.grid(row=1,column=1)

def Get_Interger_Input_In_Range(Given_String,Error_Message,Choice_Range):
    while True:
        try:
            Returned_Value=int(Generate_Entry_Box(Given_String))
            if Returned_Value<1 or Returned_Value>Choice_Range:
                Generate_Alert_Message(Error_Message)
            else:
                break
        except ValueError:
            Generate_Alert_Message(Error_Message)
    return Temp

我已经在我的代码中包含了我正在努力的所有内容,并且我可以找到答案。 I.E:单击鼠标,使用给定参数执行特定操作。 我找不到的一件事是,如何在单击按钮后将输入的值返回到原始(Get_Interger_Input_In_Range())函数。 我的意思是这样的:

def Function1(GivenParameter1,GivenParameter2):
    Temp=Function2(GivenParameter1)
    Temp+=GiverParameter2   #random action
    return Temp

def Function2(GivenParameter):
    Button=Button(Master,command=Function3).grid()
    Entry=Entry(Master).grid()

def Function3():
    Temp=Entry.get()
    return Temp

在Function1中,我希望Temp等于Function2输入的值。 有没有办法在不使用课程的情况下这样做? (我对课程还不太熟悉) 有没有办法做到这一点? 我还没有看到有人给出我正在寻找的答案...... 因为即使他们说要使用课程......我仍然不知道如何归还课程(下面的说明)

#The following code was written quickly for purposes of explaining what I mean. It doesn't actually work... (It seems that the button command is being called automatically...)

from tkinter import *
class Return_Value_In_Entry():
    def __init__(self):
        self.Master=Tk()
        self.Entry=Entry(self.Master)
        self.Button=Button(self.Master,text="Submit",command=self.Return())
    def Return(self):
        self.TempVar=self.Entry.get()
        return self.TempVar

我看到它的方式,Return()函数会将值返回给按钮,而不是调用类的函数/赋值...这与我的代码有同样的问题。< / p>

如果你读完这一切,我真的很感激。我希望有人可以回答我的问题并告诉我(如果不可能的话)如何使用课程解决我的问题&#34; Little&#34;但问题很大。

1 个答案:

答案 0 :(得分:2)

我修复了你的示例代码(我认为)。主要问题是:

command=self.Return()

不符合您的想法。它只是将Return()的返回值赋给命令。这是不正确的。它应该是

command=self.Return

这指定函数返回命令。随后,当按下任何按钮时,执行self.Return()。

完整的例子在这里:

from tkinter import *

class Return_Value_In_Entry():
    def __init__(self):
        self.Master=Tk()

        self.Entry=Entry(self.Master)
        self.Entry.pack()

        self.Button=Button(self.Master,text="Submit",command=self.Return)
        self.Button.pack()            

        self.Master.mainloop()

    def Return(self):
        self.TempVar=self.Entry.get()

        print(self.TempVar)        


Return_Value_In_Entry()  

现在,无论何时按下Button,Entry小部件中的值都会保存到self.TempVar中并打印出来,以检查它是否正常工作。希望这会有所帮助。

显示示例程序如何工作的Gif:

enter image description here