我有一个由PAGE GUI制成的GUI,它们是按钮,别名为button1。 我希望当我单击此按钮“单击”时,它在别名为entryText的textBox中显示消息“ Hello World”。 我们应该如何定义功能?我们可以在GUI文件本身中定义吗?
假设我有两个文件:new.py:(GUI文件)
import sys
import new_support
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 vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
new_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)
new_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 {Segoe UI} -size 12 -weight normal -slant " \
"roman -underline 0 -overstrike 0"
font11 = "-family {Segoe UI} -size 14 -weight normal -slant " \
"roman -underline 0 -overstrike 0"
font12 = "-family {Segoe UI} -size 12 -weight bold -slant " \
"roman -underline 1 -overstrike 0"
font14 = "-family {Segoe UI} -size 9 -weight bold -slant roman" \
" -underline 0 -overstrike 0"
font18 = "-family {Segoe UI} -size 10 -weight bold -slant " \
"roman -underline 0 -overstrike 0"
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("1470x846+133+135")
top.title("Major Project")
top.configure(background="#d9d9d9")
self.Frame1 = tk.Frame(top)
self.Frame1.place(relx=-0.007, rely=0.0, relheight=0.148, relwidth=0.52)
self.Frame1.configure(relief='groove')
self.Frame1.configure(borderwidth="2")
self.Frame1.configure(relief='groove')
self.Frame1.configure(background="#d9d9d9")
self.Frame1.configure(width=765)
self.Label1 = tk.Label(self.Frame1)
self.Label1.place(relx=0.078, rely=0.16, height=34, width=133)
self.Label1.configure(background="#d9d9d9")
self.Label1.configure(disabledforeground="#a3a3a3")
self.Label1.configure(font=font10)
self.Label1.configure(foreground="#000000")
self.Label1.configure(text='''Query:''')
self.Label1.configure(width=133)
self.Text1 = tk.Text(self.Frame1)
self.Text1.place(relx=0.431, rely=0.16, relheight=0.352, relwidth=0.384)
self.Text1.configure(background="#fff0f9")
self.Text1.configure(borderwidth="2")
self.Text1.configure(cursor="sizing")
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(relief='groove')
self.Text1.configure(selectbackground="#c4c4c4")
self.Text1.configure(selectforeground="#000000")
self.Text1.configure(width=294)
self.Text1.configure(wrap='word')
self.Button = tk.Button(self.Frame1)
self.Button.place(relx=0.314, rely=0.56, height=33, width=56)
self.Button.configure(activebackground="#ececec")
self.Button.configure(activeforeground="#000000")
self.Button.configure(background="#d9d9d9")
self.Button.configure(disabledforeground="#a3a3a3")
self.Button.configure(foreground="#000000")
self.Button.configure(highlightbackground="#d9d9d9")
self.Button.configure(highlightcolor="black")
self.Button.configure(pady="0")
self.Button.configure(text='''Search''')
self.Frame2 = tk.Frame(top)
self.Frame2.place(relx=0.51, rely=-0.012, relheight=0.16, relwidth=0.793)
self.Frame2.configure(relief='groove')
self.Frame2.configure(borderwidth="2")
self.Frame2.configure(relief='groove')
self.Frame2.configure(background="#d9d9d9")
self.Frame2.configure(width=1165)
self.Label2 = tk.Label(self.Frame2)
self.Label2.place(relx=0.352, rely=0.296, height=47, width=295)
self.Label2.configure(background="#d9d9d9")
self.Label2.configure(disabledforeground="#a3a3a3")
self.Label2.configure(font=font11)
self.Label2.configure(foreground="#000000")
self.Label2.configure(text='''Exploration Area''')
self.Label2.configure(width=295)
self.Frame3 = tk.Frame(top)
self.Frame3.place(relx=0.0, rely=0.142, relheight=1.034, relwidth=0.514)
self.Frame3.configure(relief='groove')
self.Frame3.configure(borderwidth="2")
self.Frame3.configure(relief='groove')
self.Frame3.configure(background="#d9d9d9")
self.Frame3.configure(width=755)
self.Label3 = tk.Label(self.Frame3)
self.Label3.place(relx=0.0, rely=0.034, height=44, width=173)
self.Label3.configure(background="#d9d9d9")
self.Label3.configure(disabledforeground="#a3a3a3")
self.Label3.configure(font=font12)
self.Label3.configure(foreground="#000000")
self.Label3.configure(text='''Result:''')
self.Label3.configure(width=173)
self.Frame5 = tk.Frame(self.Frame3)
self.Frame5.place(relx=0.013, rely=0.126, relheight=0.234
, relwidth=0.974)
self.Frame5.configure(relief='groove')
self.Frame5.configure(borderwidth="2")
self.Frame5.configure(relief='groove')
self.Frame5.configure(background="#d9d9d9")
self.Frame5.configure(width=735)
self.Label4 = tk.Label(self.Frame5)
self.Label4.place(relx=0.014, rely=0.049, height=26, width=82)
self.Label4.configure(background="#d9d9d9")
self.Label4.configure(disabledforeground="#a3a3a3")
self.Label4.configure(font=font14)
self.Label4.configure(foreground="#000000")
self.Label4.configure(text='''D1:''')
self.Label4.configure(width=82)
self.Label5 = tk.Label(self.Frame5)
self.Label5.place(relx=0.014, rely=0.39, height=26, width=81)
self.Label5.configure(background="#d9d9d9")
self.Label5.configure(disabledforeground="#a3a3a3")
self.Label5.configure(font=font14)
self.Label5.configure(foreground="#000000")
self.Label5.configure(text='''Abstract''')
self.Label5.configure(width=81)
self.Label6 = tk.Label(self.Frame5)
self.Label6.place(relx=0.014, rely=0.537, height=36, width=93)
self.Label6.configure(background="#d9d9d9")
self.Label6.configure(disabledforeground="#a3a3a3")
self.Label6.configure(font=font14)
self.Label6.configure(foreground="#000000")
self.Label6.configure(text='''Keywords:''')
self.Label6.configure(width=93)
self.Label12 = tk.Label(self.Frame5)
self.Label12.place(relx=0.027, rely=0.244, height=26, width=62)
self.Label12.configure(background="#d9d9d9")
self.Label12.configure(disabledforeground="#a3a3a3")
self.Label12.configure(font=font18)
self.Label12.configure(foreground="#000000")
self.Label12.configure(text='''Title''')
self.Label12.configure(width=62)
self.button1 = tk.Button(self.Frame5)
self.button1.place(relx=0.653, rely=0.244, height=33, width=86)
self.button1.configure(activebackground="#ececec")
self.button1.configure(activeforeground="#000000")
self.button1.configure(background="#d9d9d9")
self.button1.configure(command=new_support.Click)
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='''Click''')
self.button1.configure(width=86)
self.TLabel1 = ttk.Label(self.Frame5)
self.TLabel1.place(relx=0.245, rely=0.244, height=24, width=145)
self.TLabel1.configure(background="#d9d9d9")
self.TLabel1.configure(foreground="#000000")
self.TLabel1.configure(font="TkDefaultFont")
self.TLabel1.configure(relief='flat')
self.TLabel1.configure(text='''NULL''')
self.TLabel1.configure(width=145)
self.TLabel2 = ttk.Label(self.Frame5)
self.TLabel2.place(relx=0.245, rely=0.39, height=24, width=125)
self.TLabel2.configure(background="#d9d9d9")
self.TLabel2.configure(foreground="#000000")
self.TLabel2.configure(font="TkDefaultFont")
self.TLabel2.configure(relief='flat')
self.TLabel2.configure(text='''NULL''')
self.TLabel2.configure(width=125)
self.TLabel3 = ttk.Label(self.Frame5)
self.TLabel3.place(relx=0.245, rely=0.537, height=24, width=135)
self.TLabel3.configure(background="#d9d9d9")
self.TLabel3.configure(foreground="#000000")
self.TLabel3.configure(font="TkDefaultFont")
self.TLabel3.configure(relief='flat')
self.TLabel3.configure(text='''NULL''')
self.TLabel3.configure(width=135)
self.Frame6 = tk.Frame(self.Frame3)
self.Frame6.place(relx=0.013, rely=0.389, relheight=0.234
, relwidth=0.974)
self.Frame6.configure(relief='groove')
self.Frame6.configure(borderwidth="2")
self.Frame6.configure(relief='groove')
self.Frame6.configure(background="#d9d9d9")
self.Frame6.configure(cursor="fleur")
self.Frame6.configure(width=735)
self.Label7 = tk.Label(self.Frame6)
self.Label7.place(relx=0.014, rely=0.049, height=26, width=34)
self.Label7.configure(background="#d9d9d9")
self.Label7.configure(disabledforeground="#a3a3a3")
self.Label7.configure(font=font14)
self.Label7.configure(foreground="#000000")
self.Label7.configure(text='''D2:''')
self.Label8 = tk.Label(self.Frame6)
self.Label8.place(relx=0.014, rely=0.439, height=26, width=70)
self.Label8.configure(background="#d9d9d9")
self.Label8.configure(disabledforeground="#a3a3a3")
self.Label8.configure(font=font14)
self.Label8.configure(foreground="#000000")
self.Label8.configure(text='''Abstract:''')
self.Label9 = tk.Label(self.Frame6)
self.Label9.place(relx=0.014, rely=0.634, height=26, width=79)
self.Label9.configure(background="#d9d9d9")
self.Label9.configure(disabledforeground="#a3a3a3")
self.Label9.configure(font=font14)
self.Label9.configure(foreground="#000000")
self.Label9.configure(text='''Keywords:''')
self.Label13 = tk.Label(self.Frame6)
self.Label13.place(relx=0.014, rely=0.244, height=26, width=62)
self.Label13.configure(background="#d9d9d9")
self.Label13.configure(disabledforeground="#a3a3a3")
self.Label13.configure(font=font18)
self.Label13.configure(foreground="#000000")
self.Label13.configure(text='''Title''')
self.Label13.configure(width=62)
self.button2 = tk.Button(self.Frame6)
self.button2.place(relx=0.667, rely=0.293, height=33, width=76)
self.button2.configure(activebackground="#ececec")
self.button2.configure(activeforeground="#000000")
self.button2.configure(background="#d9d9d9")
self.button2.configure(command=new_support.Click)
self.button2.configure(disabledforeground="#a3a3a3")
self.button2.configure(foreground="#000000")
self.button2.configure(highlightbackground="#d9d9d9")
self.button2.configure(highlightcolor="black")
self.button2.configure(pady="0")
self.button2.configure(text='''Click''')
self.button2.configure(width=76)
self.TLabel4 = ttk.Label(self.Frame6)
self.TLabel4.place(relx=0.272, rely=0.244, height=24, width=45)
self.TLabel4.configure(background="#d9d9d9")
self.TLabel4.configure(foreground="#000000")
self.TLabel4.configure(font="TkDefaultFont")
self.TLabel4.configure(relief='flat')
self.TLabel4.configure(text='''Tlabel''')
self.TLabel5 = ttk.Label(self.Frame6)
self.TLabel5.place(relx=0.272, rely=0.439, height=24, width=45)
self.TLabel5.configure(background="#d9d9d9")
self.TLabel5.configure(foreground="#000000")
self.TLabel5.configure(font="TkDefaultFont")
self.TLabel5.configure(relief='flat')
self.TLabel5.configure(text='''Tlabel''')
self.TLabel5.configure(cursor="fleur")
self.TLabel6 = ttk.Label(self.Frame6)
self.TLabel6.place(relx=0.286, rely=0.683, height=24, width=45)
self.TLabel6.configure(background="#d9d9d9")
self.TLabel6.configure(foreground="#000000")
self.TLabel6.configure(font="TkDefaultFont")
self.TLabel6.configure(relief='flat')
self.TLabel6.configure(text='''Tlabel''')
self.Frame4 = tk.Frame(top)
self.Frame4.place(relx=0.51, rely=0.142, relheight=0.845, relwidth=0.793)
self.Frame4.configure(relief='groove')
self.Frame4.configure(borderwidth="2")
self.Frame4.configure(relief='groove')
self.Frame4.configure(background="#d9d9d9")
self.Frame4.configure(width=1165)
self.Label10 = tk.Label(self.Frame4)
self.Label10.place(relx=0.361, rely=0.042, height=56, width=322)
self.Label10.configure(background="#d9d9d9")
self.Label10.configure(disabledforeground="#a3a3a3")
self.Label10.configure(font=font12)
self.Label10.configure(foreground="#000000")
self.Label10.configure(text='''Highly Influenced Top 10''')
self.Label10.configure(width=322)
self.TFrame1 = ttk.Frame(self.Frame4)
self.TFrame1.place(relx=0.0, rely=0.797, relheight=0.315, relwidth=0.991)
self.TFrame1.configure(relief='groove')
self.TFrame1.configure(borderwidth="2")
self.TFrame1.configure(relief='groove')
self.TFrame1.configure(width=1155)
self.Label11 = tk.Label(self.TFrame1)
self.Label11.place(relx=0.39, rely=0.089, height=46, width=259)
self.Label11.configure(background="#d9d9d9")
self.Label11.configure(disabledforeground="#a3a3a3")
self.Label11.configure(font=font12)
self.Label11.configure(foreground="#000000")
self.Label11.configure(highlightcolor="#0a1363")
self.Label11.configure(text='''Topics Related Key Words''')
self.Label11.configure(width=259)
self.textbox = tk.Text(self.Frame4)
self.textbox.place(relx=0.077, rely=0.182, relheight=0.551
, relwidth=0.441)
self.textbox.configure(background="white")
self.textbox.configure(font="TkTextFont")
self.textbox.configure(foreground="black")
self.textbox.configure(highlightbackground="#d9d9d9")
self.textbox.configure(highlightcolor="black")
self.textbox.configure(insertbackground="black")
self.textbox.configure(selectbackground="#c4c4c4")
self.textbox.configure(selectforeground="black")
self.textbox.configure(width=514)
self.textbox.configure(wrap='word')
if __name__ == '__main__':
vp_start_gui()
,然后是一个名为but.py的(主)文件:
from tkinter import *
import new
import new_support
import tkinter
import tkinter.messagebox
class GUI_Functions(new.Toplevel1):
def __init__(self,parent):
new.Toplevel1.__init__(self, parent)
if __name__ == '__main__':
root = Tk()
GUI_Frame = new.Toplevel1(root)
root.mainloop()