我试图在按下按钮时使文本显示在GUI应用程序的输入框中。目的是当按下一个按钮时,在教科书中会出现一些定义的文本,而当按下另一个按钮时,将清除先前的输入框,并将不同的文本插入同一输入框。
我是Python的新手,因此不确定如何执行此操作?到目前为止,我有三个按钮可以在GUI中将每个按钮显示为不同的文本,而不是显示在单独的文本框中。有人可以帮忙吗?这是我的当前代码:
`#*****前言代码*****
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
def new():
tkinter.messagebox.showinfo('Window Title', 'Well, this is new...')
root = Tk()
root.title("GUI Test Version 2")
root.resizable(False, False)
root.geometry('{}x{}'.format(400, 400))
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Experiment...", command=new)
subMenu.add_command(label="New...", command=new)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=root.destroy)
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=new)
toolbar = Frame(root, bg="light blue")
toolbar.pack(side=TOP, fill=X)
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = Frame(self.root, width=900, height=900)
self.txt_frm.pack(fill="both", expand=True)
button1 = Button(self.txt_frm,text="HELLO", command = self.hello_world)
button1.grid(column=0,row=2, padx=2, pady=2)
button2 = Button(self.txt_frm,text="GOODBYE", command = self.goodbye_world)
button2.grid(column=1,row=2, padx=2, pady=2)
button3 = Button(self.txt_frm,text="NEW", command = self.new_world, bg="red",fg="white")
button3.grid(column=2,row=2, padx=2, pady=2)
def hello_world(self):
label = Label(self.txt_frm,text='HELLO WORLD!')
label.grid(column=0,row=3)
def goodbye_world(self):
label = Label(self.txt_frm,text='GOODBYE WORLD!')
label.grid(column=1,row=3)
def new_world(self):
label = Label(self.txt_frm,text='THIS IS A NEW WORLD!')
label.grid(column=2,row=3)
status = Label(root, text="Preparing to begin...", bd=1, relief=SUNKEN, anchor=W) # bd = bordered, relief = , appear placed in screen, anchor = w (NESW) needs two other properties
status.pack(side=BOTTOM, fill=X)
app = App(root)
root.mainloop()`
答案 0 :(得分:0)
读取和写入条目的通常方法是使用StringVar作为textvariable。检查以下代码:
from tkinter import *
root = Tk()
root.geometry('300x100')
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = Frame(self.root, width=900, height=900, bg='khaki')
self.txt_frm.pack(fill="both", expand=True)
button1 = Button(self.txt_frm,text="Hello", command = self.hello_world)
button1.grid(column=0,row=2, padx=2, pady=2)
button2 = Button(self.txt_frm,text="Goodbye", command = self.goodbye_world)
button2.grid(column=1,row=2, padx=2, pady=2)
self.entry_var = StringVar()
entry = Entry(self.txt_frm, textvariable=self.entry_var)
entry.grid(column=0, row=3, columnspan=2, padx=2, pady=2)
def hello_world(self):
self.entry_var.set('Hello')
def goodbye_world(self):
self.entry_var.set('World')
app = App(root)
root.mainloop()
我正在将StringVar self.entry_var
分配给该条目。然后,我使用按钮回调函数通过修改self.entry_var
来更改条目的内容。
答案 1 :(得分:0)
在__init__
中添加标签,并在以后使用label.config(text=text)
更改其文本。这是示例代码:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
def new():
tkinter.messagebox.showinfo('Window Title', 'Well, this is new...')
root = Tk()
root.title("GUI Test Version 2")
root.resizable(False, False)
root.geometry('{}x{}'.format(400, 400))
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Experiment...", command=new)
subMenu.add_command(label="New...", command=new)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=root.destroy)
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=new)
toolbar = Frame(root, bg="light blue")
toolbar.pack(side=TOP, fill=X)
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = Frame(self.root, width=900, height=900)
self.txt_frm.pack(fill="both", expand=True)
button1 = Button(self.txt_frm,text="HELLO", command = self.hello_world)
button1.grid(column=0,row=2, padx=2, pady=2)
button2 = Button(self.txt_frm,text="GOODBYE", command = self.goodbye_world)
button2.grid(column=1,row=2, padx=2, pady=2)
button3 = Button(self.txt_frm,text="NEW", command = self.new_world, bg="red",fg="white")
button3.grid(column=2,row=2, padx=2, pady=2)
self.label = Label(self.txt_frm,text='')
self.label.grid(column=0,row=3)
def hello_world(self):
self.label.config(text="HELLO WORLD!")
def goodbye_world(self):
self.label.config(text="GOODBYE WORLD!")
def new_world(self):
self.label.config(text="THIS IS A NEW WORLD!")
status = Label(root, text="Preparing to begin...", bd=1, relief=SUNKEN, anchor=W) # bd = bordered, relief = , appear placed in screen, anchor = w (NESW) needs two other properties
status.pack(side=BOTTOM, fill=X)
app = App(root)
root.mainloop()