我正在尝试使用self.search来确定我的.txt文件中的哪些行将被显示,即:条目为“蓝色”,它将在文本中打印所有带蓝色的行。
每个按钮都会搜索不同的.txt文件。
Tkinter回调中的异常回溯(最近一次调用最后一次):文件“C:\ Users \ gblmac \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ tkinter__init __。py”,第1702行,调用在searchvision中返回self.func(* args)文件“D:\ Users \ gblmac \ Desktop \ Python程序\ Testing \ GUI类和 init .py”,第37行self.usertext in line:TypeError:'in'要求字符串作为左操作数,而不是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()
答案 0 :(得分:2)
in
需要字符串作为左操作数
对于此
if self.usertext in line:
不是StringVar
因为左侧变量是StringVar
self.usertext = StringVar()
您需要使用
if self.usertext.get() in line: