我已经定义了一个名为changeText()的方法,它将在Button Click上显示“Hello”,我在Button中调用了它。但是我仍然无法在Textbox中获取别名为TextBox1的文本,并在Button中调用了方法,其别名是Button1,但我还没有得到结果。
我有两个文件:Button.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 Button_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
Button_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)
Button_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+639+258")
top.title("New Toplevel")
top.configure(background="#d9d9d9")
#this is defined method and I have called in button
def changeText():
self.Text1.insert(END,"hyyyy")
self.Button1 = tk.Button(top,command=changeText)
self.Button1.place(relx=0.4, rely=0.289, 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.3, rely=0.089, relheight=0.12, relwidth=0.29)
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=174)
self.Text1.configure(wrap='word')
if __name__ == '__main__':
vp_start_gui()
另一个文件是:Button_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 Button
Button.vp_start_gui()
单击按钮时,我能够让代码在文本框中显示文本:
这是你想要完成的吗?你没有提到你是否收到任何错误,但我确实发现了你的代码有两个问题:
Button.py
脚本中似乎存在一些缩进问题。self.Text1.insert(END,"hyyyy")
方法中的changeText()
,而不是END
,你可能想要tk.END
(例如,self.Text1.insert(tk.END, "hyyyy")
)。这是我的Button.py
代码的略微修改版本,似乎在我的最后运行。我没有对你的Button_support.py
代码做任何改动。
button.朋友
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 Button_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
Button_support.init(root, top)
root.mainloop()
w = None
def create_Toplevel1(root, *args, **kwargs):
global w, w_win, rt
rt = root
w = tk.Toplevel (root)
top = Toplevel1 (w)
Button_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+639+258")
top.title("New Toplevel")
top.configure(background="#d9d9d9")
#this is defined method and I have called in button
def changeText():
self.Text1.insert(tk.END,"hyyyy")
self.Button1 = tk.Button(top,command=changeText)
self.Button1.place(relx=0.4, rely=0.289, 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.3, rely=0.089, relheight=0.12, relwidth=0.29)
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=174)
self.Text1.configure(wrap='word')
if __name__ == '__main__':
vp_start_gui()
如果这不是您想要的,请说明您期望的行为,我会尝试相应地修改我的答案。