Python(Tkinter)单击按钮使文本显示在输入框中?

Python(Tkinter)单击按钮使文本显示在输入框中?,python,button,tkinter,Python,Button,Tkinter,当按下按钮时,我试图使文本显示在GUI应用程序的输入框中。其目的是,当按下一个按钮时,教科书中会出现一些定义的文本,当按下另一个按钮时,会清除上一个输入框,并将不同的文本插入同一个输入框 我是Python新手,因此不确定如何做到这一点?到目前为止,我有三个按钮来显示不同的文本,GUI中的每个按钮都是文本,而不是单独的文本框中的文本。有人能帮忙吗?这是我目前的代码: `#******前言代码***** from tkinter import * from tkinter import ttk im

当按下按钮时,我试图使文本显示在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))
*****主菜单***** *****工具栏***** *****创建按钮***** *****状态栏***** *****运行代码*****
读取和写入条目的常用方法是将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\u var
分配给条目。然后我使用按钮回调函数通过修改
self.entry\u var
来更改条目的内容

\uuuu init\uuuu
中创建一个标签,然后使用
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()

请不要在代码中穿插那些无用的标题。大多数人喜欢的是我们可以复制和粘贴的单个代码块。另外,请尝试将代码减少到一个最小值。如果一个按钮和一个条目小部件有问题,我们实际上只需要一个按钮和一个条目小部件就可以看到您正在做什么(加上足够的代码使其运行)。请提供一个代码,而不是制作标题。另外修复缩进你的代码有很多不相关的细节,比如菜单和标签,没有输入框。这非常有用,回答了我的问题。我也不确定如何保存这些条目的内容。例如,您通常会在文本编辑器应用程序中使用“保存”对话框将文本框转换为文本文件?您能帮个忙吗?目前保存的代码是:def save_file():savefile=open(“newfile.txt”,“w”)savefile.write(“输入框1:+Entry_-box1.get())savefile.write(“\Entry-Box 2:+Entry_-box2.get()”)savefile.write(“\Entry-Box 3:+Entry_-box3.get())savefile.close()但是,这不会显示“保存”对话框,也不会显示。当我重新运行代码时,旧文件被覆盖,我需要能够为每个文件指定一个文件名,并让它们与输入框中的文本一起单独保存。我该怎么做?@HenryPowell Davies,我在你的密码中没有看到任何条目。你能告诉我你在说哪个条目吗?但是,如果要获取标签的文本,请使用
self.label[“text”]
,它将以字符串形式返回文本
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()`
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()
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()