无法在 Python 3 中获取 tkinter Entry 小部件的内容
Can't get contents of tkinter Entry widget in Python 3
我从 Python 3 开始,我在从 tkinter 小部件取回值时遇到问题。当我 运行 我的代码时,我得到一个 'name edtGetMe is not defined' 错误。谁能看出我哪里出错了?
from tkinter import *
# Define the class that forms my window.
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
# Init the class.
def init_window(self):
# Place widgets on the window.
self.pack(fill=BOTH, expand=1)
# Quit button.
btnCancel = Button (self, text="Cancel", command=self.cancel_out)
btnCancel.place (x=10, y=120)
# Action button.
btnAction = Button (self, text="Set Text", command=self.action)
btnAction.place (x=100, y=120)
# The Editboxes.
edtGetMe = Entry (self)
edtSetMe = Entry (self)
edtGetMe.place (y=10, x=10, width=380, height=100)
edtSetMe.place (y=155, x=10, width=380, height=100)
def cancel_out(self):
exit()
def action (self):
# Get the entry from the GetMe box.
self.usrText = edtGetMe.get()
# Stuff it into the other box.
edtSetMe.insert(self.usrText)
def main():
root = Tk()
# Size of window.
root.geometry("400x300")
# Start Window Class.
app = Window(root)
root.mainloop()
if name == "main":
主()
首先,我将 edtGetMe
替换为 self.edtGetMe
,edtSetMe
也是如此,因此这些变量可从所有 class 函数访问。然后我在你的行 self.edtSetMe.insert("insert", self.usrText)
中添加了 "insert"。您还有缩进问题。
试试这个:
from tkinter import *
# Define the class that forms my window.
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
# Init the class.
def init_window(self):
# Place widgets on the window.
self.pack(fill=BOTH, expand=1)
# Quit button.
btnCancel = Button (self, text="Cancel", command=self.cancel_out)
btnCancel.place (x=10, y=120)
# Action button.
btnAction = Button (self, text="Set Text", command=self.action)
btnAction.place (x=100, y=120)
# The Editboxes.
self.edtGetMe = Entry (self)
self.edtSetMe = Entry (self)
self.edtGetMe.place (y=10, x=10, width=380, height=100)
self.edtSetMe.place (y=155, x=10, width=380, height=100)
def cancel_out(self):
exit()
def action (self):
# Get the entry from the GetMe box.
self.usrText = self.edtGetMe.get()
# Stuff it into the other box.
self.edtSetMe.insert("insert", self.usrText)
def main():
root = Tk()
# Size of window.
root.geometry("400x300")
# Start Window Class.
app = Window(root)
root.mainloop()
if __name__ == '__main__':
main()
我从 Python 3 开始,我在从 tkinter 小部件取回值时遇到问题。当我 运行 我的代码时,我得到一个 'name edtGetMe is not defined' 错误。谁能看出我哪里出错了?
from tkinter import *
# Define the class that forms my window.
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
# Init the class.
def init_window(self):
# Place widgets on the window.
self.pack(fill=BOTH, expand=1)
# Quit button.
btnCancel = Button (self, text="Cancel", command=self.cancel_out)
btnCancel.place (x=10, y=120)
# Action button.
btnAction = Button (self, text="Set Text", command=self.action)
btnAction.place (x=100, y=120)
# The Editboxes.
edtGetMe = Entry (self)
edtSetMe = Entry (self)
edtGetMe.place (y=10, x=10, width=380, height=100)
edtSetMe.place (y=155, x=10, width=380, height=100)
def cancel_out(self):
exit()
def action (self):
# Get the entry from the GetMe box.
self.usrText = edtGetMe.get()
# Stuff it into the other box.
edtSetMe.insert(self.usrText)
def main():
root = Tk()
# Size of window.
root.geometry("400x300")
# Start Window Class.
app = Window(root)
root.mainloop()
if name == "main": 主()
首先,我将 edtGetMe
替换为 self.edtGetMe
,edtSetMe
也是如此,因此这些变量可从所有 class 函数访问。然后我在你的行 self.edtSetMe.insert("insert", self.usrText)
中添加了 "insert"。您还有缩进问题。
试试这个:
from tkinter import *
# Define the class that forms my window.
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
# Init the class.
def init_window(self):
# Place widgets on the window.
self.pack(fill=BOTH, expand=1)
# Quit button.
btnCancel = Button (self, text="Cancel", command=self.cancel_out)
btnCancel.place (x=10, y=120)
# Action button.
btnAction = Button (self, text="Set Text", command=self.action)
btnAction.place (x=100, y=120)
# The Editboxes.
self.edtGetMe = Entry (self)
self.edtSetMe = Entry (self)
self.edtGetMe.place (y=10, x=10, width=380, height=100)
self.edtSetMe.place (y=155, x=10, width=380, height=100)
def cancel_out(self):
exit()
def action (self):
# Get the entry from the GetMe box.
self.usrText = self.edtGetMe.get()
# Stuff it into the other box.
self.edtSetMe.insert("insert", self.usrText)
def main():
root = Tk()
# Size of window.
root.geometry("400x300")
# Start Window Class.
app = Window(root)
root.mainloop()
if __name__ == '__main__':
main()