从不同类的条目中获取值

时间:2018-12-11 10:34:11

标签: python tkinter

所以我试图从另一个类的条目中获取值,但我无法使其起作用,我试图使代码尽可能短,以便希望你们能为我提供帮助

import tkinter as tk
import time
import threading

class lingo(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        m = tk.Frame(self)
        m.grid(row=0, column=0,sticky="nsew")

        e1 = tk.Entry(m)
        e1.grid(row=0, column=0)

        def enter():
            entry1 = e1.get()
            t1 = threading.Thread(target=check)
            t1.start()

        b1 = tk.Button(m, bg="blue", text="enter", command=enter)
        b1.grid(row=0, column=1)

class check():
    def __init__(self):
            lingo1 = lingo()
            entry2 = lingo1.enter()
            entry3 = entry2.entry1()
            print(entry3)

t0 = threading.Thread(target=lingo)
t0.start()  
app = lingo()
app.wm_geometry("1810x1080")
app.resizable(False, False)
app.title("19 LetterWoordenLingo")
app.mainloop()

3 个答案:

答案 0 :(得分:0)

只需通过__init__方法移交变量:

    (...)
    def enter():
        entry1 = e1.get()
        t1 = threading.Thread(target=check(entry1))
        t1.start()
    (...)    

class check():
    def __init__(self, entry1):
            (...)

答案 1 :(得分:0)

您可以尝试在check类的初始化中调用lingo类,然后创建对象。

class check(lingo):
    def __init__(self):
        lingo.__init__(self)
        lingo1 = ClassA() 
        entry2 = lingo1.enter()
        entry3 = entry2.entry1()
        print(entry3)

我希望这会有所帮助。

答案 2 :(得分:0)

我认为,您应该稍微组织一下代码。这是一个有效的示例:

import tkinter as tk
import time
import threading


class Lingo(tk.Tk):
    def __init__(self, *args, **kwargs):
      tk.Tk.__init__(self, *args, **kwargs)

      self.m = tk.Frame(self)
      self.m.grid(row=0, column=0,sticky="nsew")

      self.e1 = tk.Entry(self.m)
      self.e1.grid(row=0, column=0)

      b1 = tk.Button(self.m, bg="blue", text="enter", command= lambda :self.enter())
      b1.grid(row=0, column=1)

    def enter(self):
      object_check = Check(parent=self)
      t1 = threading.Thread(target=object_check.get_entry)
      t1.start()

class Check():
    def __init__(self, parent):
      self.parent = parent

    def get_entry(self):
      entry_text = self.parent.e1.get()
      print(entry_text)

if __name__ == '__main__':

  t0 = threading.Thread(target=Lingo)
  t0.start()
  app = Lingo()
  app.wm_geometry("1810x1080")
  app.resizable(False, False)
  app.title("19 LetterWoordenLingo")
  app.mainloop()

因此,您需要初始化小部件。然后按下按钮,可以创建Check类的对象,然后该对象用于通过新线程访问该类中定义的功能。

我希望它会有所帮助。