大佬教程收集整理的这篇文章主要介绍了有类时从 tkinter Python 表单获取输入变量,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这段代码可以工作,因为它在 Python 3 和 tkinter 中创建了一个非常基本的表单。我想要捕获的是输入到表单文本框中的文本(通过按下按钮)。我的问题是我对类的误解,因为我无法为爱或金钱获得变量,它似乎总是未定义的。所以我的第一个问题是获得这个的正确方法是什么,所以按钮打印变量,其次我在这里错过了什么?将变量放在一个不能在它们外部访问的类中背后的想法是什么,或者确保它们的正确方法是什么?
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,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,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 添加到我想在类外访问的每个变量中,然后添加到函数中。我不会将这个问题标记为已回答,因为 acw1668 提供的解决方案没有解释原因。 “根”甚至“任何东西”会起作用而不是“自我”。 “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,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,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()
以上是大佬教程为你收集整理的有类时从 tkinter Python 表单获取输入变量全部内容,希望文章能够帮你解决有类时从 tkinter Python 表单获取输入变量所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。