简体   繁体   English

将文本从函数传递到gui

[英]Passing text from a function to a gui

I've got a GUI function in a file 'test_gui.py', which is adapted from one of Bryan Oakley's answers to a question regarding getting text from a Tkinter entry box. 我在文件“ test_gui.py”中有一个GUI函数,该函数改编自布莱恩·奥克利(Bryan Oakley)关于从Tkinter输入框获取文本的问题的答案之一。

  import sys
  import os
  import Tkinter as tk

  class testing(tk.Tk):
      def __init__(self):
        tk.Tk.__init__(self)
        self.label1 = tk.Label(self, text = "Enter benchmark version")
        self.label2 = tk.Label(self, text = "Enter test_suite (a for all)")
        self.label3 = tk.Label(self, text = "Enter sub_suite t or w")
        self.entry1 = tk.Entry(self)
        self.entry2 = tk.Entry(self)
        self.entry3 = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.grid(row = 4, column = 0)
        self.label1.grid(row = 1, column = 0)
        self.label2.grid(row = 2, column = 0)
        self.label3.grid(row = 3, column = 0)
        self.entry1.grid(row = 1, column = 1)
        self.entry2.grid(row = 2, column = 1)
        self.entry3.grid(row = 3, column = 1)

     def on_button(self):

        benchmark = self.entry1.get()
        test_suite = self.entry2.get()
        sub_suite = self.entry3.get()
        home_path=os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(home_path, sub_suite, 'results')
        sys.path.insert(0, path)
        import compare_data as compare
        compare.compare_results(benchmark, test_suite)
        self.label4 = tk.Label(self, text=fil)
        self.button2.grid(row = 5, column = 10)
app = testing()
app.mainloop()

and I need to pass it 'fil' from a different function which is run after pressing button through the function compare_results. 我需要从另一个函数传递它的“ fil”,该函数在按下按钮后通过compare_results函数运行。 In this function I've got: 在此功能中,我得到了:

 import test_gui

 test_gui.testing(fil)

To do this I think I need to define on_button as 为此,我认为我需要将on_button定义为

  def on_button(self, fil)

But then this returns the error that on_button requires two arguments. 但这然后返回错误,即on_button需要两个参数。 If I give fil a default value it will pass that to the label on pressing the button. 如果我给fil一个默认值,它将在按下按钮时将其传递给标签。

Is there a way of passing text from a function run through a gui back to the gui? 有没有一种方法可以将文本从通过gui运行的函数传回gui?

You can use lambda to pass more arguments to the button command. 您可以使用lambda将更多参数传递给button命令。 So instead of 所以代替

command = self.on_button

You could use 你可以用

command = lambda: self.on_button(fil)

To pass 'fil' to the on_button function. 将“ fil”传递给on_button函数。 Is this what you had in mind? 这就是您的想法吗?

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

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