[英]Get input variable from tkinter Python form when there is a class
我有这段代码,它在 Python 3 和 tkinter 中创建了一个非常基本的形式。 我要捕获的是输入到表单文本框中的文本(通过按下按钮)。 我的问题是我对课程的误解,因为我无法为爱或金钱获得变量,它似乎总是未定义。 所以我的第一个问题是得到这个的正确方法是什么,所以按钮打印变量,其次我在这里缺少什么? 将变量放入无法在外部访问的 class 中的想法是什么,或者确保它们的正确方法是什么?
import tkinter as tk
import tkinter.font as tkFont
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class App:
def __init__(self, root):
#setting title
root.title("undefined")
#setting window size
width=600
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
GButton_815=tk.Button(root)
GButton_815["bg"] = "#efefef"
ft = tkFont.Font(family='Times',size=10)
GButton_815["font"] = ft
GButton_815["fg"] = "#000000"
GButton_815["justify"] = "center"
GButton_815["text"] = "Run"
GButton_815.place(x=50,y=100,width=70,height=25)
GButton_815["command"] = self.GButton_815_command
GLineEdit_786=tk.Entry(root)
GLineEdit_786["borderwidth"] = "1px"
ft = tkFont.Font(family='Times',size=10)
GLineEdit_786["font"] = ft
GLineEdit_786["fg"] = "#333333"
GLineEdit_786["justify"] = "left"
GLineEdit_786["text"] = "Entry"
GLineEdit_786.place(x=190,y=100,width=260,height=30)
GLabel_146=tk.Label(root)
ft = tkFont.Font(family='Times',size=10)
GLabel_146["font"] = ft
GLabel_146["fg"] = "#333333"
GLabel_146["justify"] = "center"
GLabel_146["text"] = "Video ID"
GLabel_146.place(x=170,y=70,width=70,height=25)
def GButton_815_command():
vidIQInput = GLineEdit_786.get()
print(vidIQInput)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
正是这段代码打败了我
def GButton_815_command():
vidIQInput = GLineEdit_786.get()
print(vidIQInput)
总是给出这个结果
vidIQInput = GLineEdit_786.get()
NameError: name 'GLineEdit_786' is not defined
我错过了什么?
答案是将self添加到我想在class之外访问的每个变量,然后添加到function。 我不会将此问题标记为已回答,因为 acw1668 提供的解决方案没有解释原因。 “root”甚至“anything”会代替“self”起作用。 在 class 的上下文中,“self”和“root”是什么意思?
import tkinter as tk
import tkinter.font as tkFont
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class App:
def __init__(self, root):
#setting title
root.title("undefined")
#setting window size
width=600
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
self.GButton_815=tk.Button(root)
self.GButton_815["bg"] = "#efefef"
ft = tkFont.Font(family='Times',size=10)
self.GButton_815["font"] = ft
self.GButton_815["fg"] = "#333333"
self.GButton_815["justify"] = "center"
self.GButton_815["text"] = "Run"
self.GButton_815.place(x=50,y=100,width=70,height=25)
self.GButton_815["command"] = self.GButton_815_command
self.GLineEdit_786=tk.Entry(root)
self.GLineEdit_786["borderwidth"] = "1px"
ft = tkFont.Font(family='Times',size=10)
self.GLineEdit_786["font"] = ft
self.GLineEdit_786["fg"] = "#333333"
self.GLineEdit_786["justify"] = "left"
self.GLineEdit_786["text"] = "Entry"
self.GLineEdit_786.place(x=190,y=100,width=260,height=30)
GLabel_146=tk.Label(root)
ft = tkFont.Font(family='Times',size=10)
GLabel_146["font"] = ft
GLabel_146["fg"] = "#333333"
GLabel_146["justify"] = "center"
GLabel_146["text"] = "Video ID"
GLabel_146.place(x=170,y=70,width=70,height=25)
def GButton_815_command(self):
vidIQInput = self.GLineEdit_786.get()
print(vidIQInput)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.