我定义了一个名为changeText()的方法,该方法将在Button Click上显示“ Hello”,而我在Button中调用了它。 但是仍然无法在别名为TextBox1的Textbox中获取文本,并在别名为Button1的Button中调用了方法,但仍然没有得到结果。
我有两个文件: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()
答案 0 :(得分:0)
当单击按钮时,我能够使您的代码在文本框中显示文本:
这是您要完成的事情吗?您没有提到是否遇到任何错误,但是我确实发现了您的代码有两个问题:
Button.py
脚本中似乎存在一些缩进问题。self.Text1.insert(END,"hyyyy")
方法中的changeText()
,您可能想要END
(例如tk.END
)来代替self.Text1.insert(tk.END, "hyyyy")
。这是我对Button.py
代码进行的稍微修改的版本,似乎在我的一端运行。我没有对您的Button_support.py
代码进行任何更改。
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):
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()
如果这不是您想要的,请说明您期望的行为,我将尝试相应地修改答案。