Tkinter 按钮向消息添加行
Tkinter button add line to message
我正在尝试使用 tkinter 在 python 中制作一个简单的图形用户界面应用程序。有一个消息字段、一个条目和一个按钮。我正在尝试为将条目中的文本发送到消息字段然后清除条目的按钮编写命令。这是我的代码:
from tkinter import *
from tkinter.ttk import *
class window(Frame):
def __init__(self, parent):
messageStr="Hello and welcome to my incredible chat program its so awesome"
Frame.__init__(self, parent)
self.parent = parent
self.parent.title("Hello World")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand="yes")
lfone = Frame(self)
lfone.pack(fill=BOTH, expand="yes")
myentry = Entry(self).pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT).pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand).pack(side=RIGHT)
def sendbuttoncommand(*args):
messager.text = messager.text + myentry.get()
myentry.delete(first, last=None)
def main():
root = Tk()
root.geometry("300x200+300+300")
app=window(root)
root.mainloop()
if __name__ == '__main__':
main()
我尝试了几种 sendbuttoncommand 变体,包括将其嵌套在 def init 中,并将 messager 和我的条目更改为 self。messager/myentry 但没有成功。就目前而言,当我尝试 运行 它时,我在 messager 上收到一个名称错误。如何影响方法范围之外的变量?我希望避免在这种情况下使用全局变量。
在您的 sendbuttoncommand
方法中,messager
未定义,因为它仅在 __init__
中本地定义。这就是导致 NameError 的原因。
如果您想在 sendbuttoncommand
方法中重复使用 messager
,只需在 __init__
中将其定义为 self.messager
使其成为实例的参数通过在 sendbuttoncommand
.
中调用 self.messager
的方法
但是,我怀疑您之后还会遇到其他错误。
除了@JulienSpronock anwser,你还需要知道写这个的时候:
myentry = Entry(self).pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT).pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand).pack(side=RIGHT)
myentry
、messager
和sendbutton
都是None
。不要这样做。应该是:
myentry = Entry(self)
myentry.pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT)
messager.pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand)
sendbutton.pack(side=RIGHT)
原因是pack()
(或grid()
)returnsNone
.
我正在尝试使用 tkinter 在 python 中制作一个简单的图形用户界面应用程序。有一个消息字段、一个条目和一个按钮。我正在尝试为将条目中的文本发送到消息字段然后清除条目的按钮编写命令。这是我的代码:
from tkinter import *
from tkinter.ttk import *
class window(Frame):
def __init__(self, parent):
messageStr="Hello and welcome to my incredible chat program its so awesome"
Frame.__init__(self, parent)
self.parent = parent
self.parent.title("Hello World")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand="yes")
lfone = Frame(self)
lfone.pack(fill=BOTH, expand="yes")
myentry = Entry(self).pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT).pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand).pack(side=RIGHT)
def sendbuttoncommand(*args):
messager.text = messager.text + myentry.get()
myentry.delete(first, last=None)
def main():
root = Tk()
root.geometry("300x200+300+300")
app=window(root)
root.mainloop()
if __name__ == '__main__':
main()
我尝试了几种 sendbuttoncommand 变体,包括将其嵌套在 def init 中,并将 messager 和我的条目更改为 self。messager/myentry 但没有成功。就目前而言,当我尝试 运行 它时,我在 messager 上收到一个名称错误。如何影响方法范围之外的变量?我希望避免在这种情况下使用全局变量。
在您的 sendbuttoncommand
方法中,messager
未定义,因为它仅在 __init__
中本地定义。这就是导致 NameError 的原因。
如果您想在 sendbuttoncommand
方法中重复使用 messager
,只需在 __init__
中将其定义为 self.messager
使其成为实例的参数通过在 sendbuttoncommand
.
self.messager
的方法
但是,我怀疑您之后还会遇到其他错误。
除了@JulienSpronock anwser,你还需要知道写这个的时候:
myentry = Entry(self).pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT).pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand).pack(side=RIGHT)
myentry
、messager
和sendbutton
都是None
。不要这样做。应该是:
myentry = Entry(self)
myentry.pack(side=LEFT, fill=BOTH, expand="yes", padx=5)
messager = Message(lfone, text=messageStr, anchor=S+W, justify=LEFT)
messager.pack(fill=BOTH, expand="yes", padx=5, pady=5)
sendbutton = Button(self, text="Send", command=self.sendbuttoncommand)
sendbutton.pack(side=RIGHT)
原因是pack()
(或grid()
)returnsNone
.