【问题标题】:How to get the setText value in Textbox Entry when Button is clicked in Page GUI python?在Page GUI python中单击按钮时如何获取文本框条目中的setText值?
【发布时间】:2019-02-23 13:13:12
【问题描述】:

我使用Page GUI Builder 构建GUI,并创建了一个button(alias:Button1) 和一个TextBox(alias:Text1)。我希望当我单击按钮时,我定义的一些文本(例如“Hello World”)会显示在文本框中。

实际上,我遇到了与将构建器定义的按钮和文本框与我要显示的文本绑定相关的问题。

注意:我正在寻找通过Page Builder 集成GUI 的解决方案。

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 UI_support

def vp_start_gui():
'''Starting point when module is the main routine.'''
  global val, w, root
  root = tk.Tk()
  top = Toplevel1 (root)
  UI_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)
  top = Toplevel1 (w)
  UI_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' 

    top.geometry("600x450+1085+305")
    top.title("New Toplevel")
    top.configure(background="#d9d9d9")

    self.Button1 = tk.Button(top)
    self.Button1.place(relx=0.417, rely=0.311, height=33, width=56)
    self.Button1.configure(activebackground="#ececec")
    self.Button1.configure(activeforeground="#000000")
    self.Button1.configure(background="#d9d9d9")
    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''')

    self.Text1 = tk.Text(top)
    self.Text1.place(relx=0.283, rely=0.133, relheight=0.12, relwidth=0.323)
    self.Text1.configure(background="white")
    self.Text1.configure(font="TkTextFont")
    self.Text1.configure(foreground="black")
    self.Text1.configure(highlightbackground="#d9d9d9")
    self.Text1.configure(highlightcolor="black")
    self.Text1.configure(insertbackground="black")
    self.Text1.configure(selectbackground="#c4c4c4")
    self.Text1.configure(selectforeground="black")
    self.Text1.configure(width=194)
    self.Text1.configure(wrap='word')

if __name__ == '__main__':
  vp_start_gui()

然后是 UI_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 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 UI
  UI.vp_start_gui()

如果我可以在 UI.py 中定义一个类,以便在单击按钮时在文本框中显示文本,或者创建一个新的 .py 文件并导入它。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您要做的主要是定义一个函数(打印消息),然后通过在按钮中使用command=(functionname) 调用该函数来激活该函数。

    所以你的函数可能看起来像这样:

    def show_text():
        print("Hello World!")
    

    您的按钮类似于:

    Button1 = tk.Button((pos), text="Hello", command=show_text) #position can be top/bottom/left or right
    

    如果需要,您可以使用 tkinter 消息框(而不是从头开始创建文本框)来显示您的消息。它可能会使事情变得更简单吗?您必须导入 tkMessageBox

    一个简短的代码示例(您的代码中已经有一些行,例如导入):

    import Tkinter as tk
    import tkMessageBox
    
    top = tk.Tk()
    
    def msg_text():
       tkMessageBox.showinfo( "Hello!", "Hello World") #arguments are title, message
    
    Button1 = tk.Button(top, text ="Hello!", command = msg_text)
    
    Button1.pack()
    top.mainloop()
    

    你没有在你的文本框中设置任何默认文本(例如 text.insert(INSERT, "Your text here")。你可以编写一个函数来设置这个配置点击要么 例如

    def onclick():
        Text1.config(text='Hello World')
    

    希望对你有帮助

    【讨论】:

    • 我的按钮和文本框已在 UI.py 文件中定义。我想添加此功能。我无法使用我想定义的功能处理 (self.button1)。我知道定义的创建部分。我在绑定以前由 GUI builder 定义的 UI 时遇到问题。
    猜你喜欢
    • 2019-07-17
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多