我已经使用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文件并将其导入。
答案 0 :(得分:0)
要做的要点是定义一个函数(打印消息),然后通过在按钮中用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,“您的文本在这里”)。您可以编写一个函数来在单击任一菜单时设置此配置 例如
def onclick():
Text1.config(text='Hello World')
希望这对您有帮助