[英]in <string>' requires string as left operand, not StringVar
I'm trying to use the self.search to determine which lines from my .txt file will be diplayed, ie: entry is "Blue" and it will print all lines with Blue in the text. 我正在尝试使用self.search来确定将显示.txt文件中的哪些行,即:entry为“ Blue”,它将在文本中打印所有带有Blue的行。
Each button searches a different .txt file. 每个按钮搜索不同的.txt文件。
Exception in Tkinter callback Traceback (most recent call last): File "C:\\Users\\gblmac\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter__init__.py", line 1702, in call return self.func(*args) File "D:\\Users\\gblmac\\Desktop\\Python programs\\Testing\\GUI class and init .py", line 37, in searchvision if self.usertext in line: TypeError: 'in ' requires string as left operand, not StringVar
Tkinter回调Traceback中的异常(最近一次调用最近):文件“ C:\\ Users \\ gblmac \\ AppData \\ Local \\ Programs \\ Python \\ Python36-32 \\ lib \\ tkinter__init __。py”,行1702,在调用返回self.func( * args)如果search.vision中的self.usertext行:TypeError:'in'中要求字符串作为左操作数,则文件“ D:\\ Users \\ gblmac \\ Desktop \\ Python program \\ Testing \\ GUI class and init .py”,第37行,不是StringVar
from tkinter import *
class App:
def __init__(self):
self.master = Tk()
self.master.title("PM and Vision Finder")
self.master.configure(bg="orange")
self.master.geometry("500x500")
self.mybutton1 = Button(self.master, text="Vision Names", command=self.searchvision)
self.mybutton1.grid(row=0, column=0)
self.mybutton2 = Button(self.master, text="PM",
command=self.searchpm)
self.mybutton2.grid(row=1, column=0)
self.usertext = StringVar()
self.initialtext = StringVar()
self.initialtext.set ("Type Here")
self.myentry = Entry(self.master, textvariable=self.initialtext)
self.myentry.grid(row=2, column=0)
def searchvision(self):
self.search = open(r"D:\Users\gblmac\Desktop\Python programs\Vision Names\Vision Names.txt")
for line in self.search:
if self.usertext in line:
print (line)
def searchpm(self):
self.search = open(r"D:\Users\gblmac\Desktop\Python programs\PMs\PMs.txt")
for line in self.search:
if self.usertext in line:
print (line)
print (self.usertext.get())
self.master.mainloop()
App()
in
requires string as left operandin
需要将字符串作为左操作数
For this 为了这
if self.usertext in line:
not StringVar
不是StringVar
Because the left side variable is a StringVar 因为左侧变量是StringVar
self.usertext = StringVar()
You need to use 您需要使用
if self.usertext.get() in line:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.