在GUI代码和函数位于两个不同文件中的实例中,如何在单击按钮时更改tk小部件

时间:2019-06-16 12:24:30

标签: python tkinter tcl

我是新来的Python编程。我有一个任务要完成一个python程序。我现在遇到了问题。我找到了页面GUI生成器,并用于设计GUI。您可能已经知道它会创建两个文件。一个包含GUI详细信息,另一个支持文件包含功能。

现在我想通过单击按钮来更改tk条目的状态。我可以在同一GUI文件中编写函数。但是我想通过在支持文件中编写函数来做到这一点。我在这里做了一个简单的例子来解释我的问题。这不是原始文件。

这是Example4.py

      import sys

  try:
      import Tkinter as tk
  except ImportError:
      import tkinter as tk

  try:
      import ttk
      py3 = False
  except ImportError:
      import tkinter.ttk as ttk
      py3 = True

  import Example4_support

  def vp_start_gui():
      '''Starting point when module is the main routine.'''
      global val, w, root
      root = tk.Tk()
      Example4_support.set_Tk_var()
      top = Toplevel1 (root)
      Example4_support.init(root, top)
      root.mainloop()

  w = None
  def create_Toplevel1(root, *args, **kwargs):
      '''Starting point when module is imported by another program.'''
      global w, w_win, rt
      rt = root
      w = tk.Toplevel (root)
      Example4_support.set_Tk_var()
      top = Toplevel1 (w)
      Example4_support.init(w, top, *args, **kwargs)
      return (w, top)

  def destroy_Toplevel1():
      global w
      w.destroy()
      w = None

  class Toplevel1:
      def __init__(self, top=None):
          '''This class configures and populates the toplevel window.
             top is the toplevel containing window.'''
          _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
          _fgcolor = '#000000'  # X11 color: 'black'
          _compcolor = '#d9d9d9' # X11 color: 'gray85'
          _ana1color = '#d9d9d9' # X11 color: 'gray85'
          _ana2color = '#ececec' # Closest X11 color: 'gray92'
          font10 = "-family {Courier New} -size 10 -weight normal -slant"  \
              " roman -underline 0 -overstrike 0"

          top.geometry("600x450+316+154")
          top.title("New Toplevel")
          top.configure(background="#d9d9d9")
          top.configure(highlightbackground="#d9d9d9")
          top.configure(highlightcolor="black")

          self.Frame1 = tk.Frame(top)
          self.Frame1.place(relx=0.05, rely=0.089, relheight=0.784, relwidth=0.922)

          self.Frame1.configure(relief='groove')
          self.Frame1.configure(borderwidth="2")
          self.Frame1.configure(relief="groove")
          self.Frame1.configure(background="#d9d9d9")
          self.Frame1.configure(highlightbackground="#d9d9d9")
          self.Frame1.configure(highlightcolor="black")
          self.Frame1.configure(width=435)

          self.Entry1 = tk.Entry(self.Frame1)
          self.Entry1.place(relx=0.253, rely=0.085,height=20, relwidth=0.297)
          self.Entry1.configure(background="white")
          self.Entry1.configure(disabledforeground="#a3a3a3")
          self.Entry1.configure(font=font10)
          self.Entry1.configure(foreground="#000000")
          self.Entry1.configure(highlightbackground="#d9d9d9")
          self.Entry1.configure(highlightcolor="black")
          self.Entry1.configure(insertbackground="black")
          self.Entry1.configure(selectbackground="#c4c4c4")
          self.Entry1.configure(selectforeground="black")
          self.Entry1.configure(state='readonly')
          self.Entry1.configure(textvariable=Example4_support.en1)

          self.Button1 = tk.Button(self.Frame1)
          self.Button1.place(relx=0.344, rely=0.255, height=24, width=47)
          self.Button1.configure(activebackground="#ececec")
          self.Button1.configure(activeforeground="#000000")
          self.Button1.configure(background="#d9d9d9")
          self.Button1.configure(command=Example4_support.btn_clicked)
          self.Button1.configure(disabledforeground="#a3a3a3")
          self.Button1.configure(foreground="#000000")
          self.Button1.configure(highlightbackground="#d9d9d9")
          self.Button1.configure(highlightcolor="black")
          self.Button1.configure(pady="0")
          self.Button1.configure(text='''Button''')

  if __name__ == '__main__':
      vp_start_gui()

这是Example4_support.py

import sys

try:
import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

def set_Tk_var():
    global en1
    en1 = tk.StringVar()

def btn_clicked():


def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top

def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None

if __name__ == '__main__':
    import Example4
    Example4.vp_start_gui()

0 个答案:

没有答案