简体   繁体   English

使用GUI输入变量并在python中运行脚本

[英]Using a GUI to input variables and run a script in python

I am currently writing a CFD program in python, the script uses a lot of predefined global variables in the calculations. 我目前正在用python编写CFD程序,该脚本在计算中使用了许多预定义的全局变量。

I would like to produce a GUI which allows the user to input all these variables, then runs the script and returns the results in the main console. 我想制作一个GUI,它允许用户输入所有这些变量,然后运行脚本并在主控制台中返回结果。 I have tried using Tkinter to do this but can't seen to find a way to set global variables. 我曾尝试使用Tkinter来执行此操作,但看不到找到设置全局变量的方法。

Below is a simple GUI I have tried designing to complete a much simpler calculation that also needs the global variables to be set. 下面是我尝试设计的简单GUI,以完成更简单的计算,该计算也需要设置全局变量。

'''GUI 2'''
import Tkinter
factor = 10

def GUIrun(n):
    return n * factor

class simpleGUI(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent=parent

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>",self.OnPressEnter)
        self.entryVariable.set("n")

        button=Tkinter.Button(self,text="Run",command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.labelVariable = Tkinter.StringVar()
        label=Tkinter.Label(self,textvariable=self.labelVariable,
        anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW')
        self.labelVariable.set("Hello!")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        n = self.entryVariable.get()
        self.labelVariable.set("n = " +self.entryVariable.get())
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)


    def OnButtonClick(self):
        n=self.entryVariable.get()
        return GUIrun(n)


if __name__=="__main__":
    app=simpleGUI(None)
    app.title('poster')
    app.initialize()
    app.mainloop()

I would appreciate any help Thank you 我将不胜感激,谢谢

Tkinter is no different than any other module with respect to global variables. 关于全局变量,Tkinter与其他模块没有什么不同。 Just declare a variable as global, then set it to some value. 只需将变量声明为全局变量,然后将其设置为某个值即可。

Your code is working, though I'm not sure it's doing what you want it to do. 您的代码正在运行,尽管我不确定它是否在执行您想要的操作。 If you want to do math, you need to convert the input value to an integer: 如果要进行数学运算,则需要将输入值转换为整数:

def OnButtonClick(self):
    n=int(self.entryVariable.get())
    ...

Your other option is to use an IntVar rather than a StringVar which will do the conversion for you. 您的另一个选择是使用IntVar而不是StringVar ,它将为您完成转换。

The other problem seems to be a fundamental misunderstanding of how Tkinter works. 另一个问题似乎是对Tkinter工作原理的根本误解。 Your OnButtonClick function correctly calls the function with the appropriate argument, then returns the result. 您的OnButtonClick函数正确调用带有适当参数的函数,然后返回结果。 Where do you think it returns it to? 您认为它返回哪里? There's no code anywhere that is expecting a return value from a button command. 在任何地方都没有期望从按钮命令返回值的代码。

You need to do something with the result -- either print it out, pass it to another function, or set a variable. 您需要对结果做些事情-将其打印出来,传递给另一个函数或设置一个变量。 Calling return in a button callback is akin to throwing the result away. 在按钮回调中调用return类似于丢弃结果。

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

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