tkinter按钮上的功能操作和标签上的结果



我有这个函数,我需要为它构建一个接口。它是一个在日志文件中搜索某些数据的函数。这些数据需要打印到标签中。1.数据(int)需要是用户输入。2.将在日志文件中搜索用户输入的值3.日志的结果需要打印在标签上。

感谢您的支持。我是新来的!大家好。

Namaste!这就是进步-请参阅下面的代码:

def searcher():
imei = input("Insert imei: ")
log = reversed(list(open("C:/test.log")))
if len(imei) == 15:
for line in log:
if imei in line:
if ("[S/W Upgrade]") in line:
print (line,"S/W upgrade found in LOG- OK to close the JOB")
elif ("SVC Connection") in line:
print(" SVC connexion only FOUND! Please connect device again for S/W Upgrade!")
else:
print(line,"FAIL")
break
else:
print("IMEI not found in LOG FILE - please connect device to FENRIR for S/W upgrade")
else:
print("Wrong IMEI number- please check your input!")
return()
searcher()

Please see below my progress:
import tkinter as tk

class ImeiApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Check IMEI", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
self.button = tk.Button(self,command=searcher)
def searcher():
global line
global searcher
imei = tk.Entry
log = reversed(list(open('C:/test.log','r')))
if len(imei) == 15:
for line in log:
if imei in line:
if ("[S/W Upgrade]") in line:
print (line,"S/W upgrade found in LOG- OK to close the JOB")
elif ("SVC Connection") in line:
print(" SVC connexion only FOUND! Please connect device again for S/W Upgrade!")
else:
print(line,"FAIL")
break
else:
print("IMEI not found in LOG FILE - please connect device to FENRIR for S/W upgrade")
else:
print("Wrong IMEI number- please check your input!")
return()
app = ImeiApp()
app.mainloop()
self.button.pack()
self.entry.pack()

您的主要问题似乎是您的按钮没有按预期执行searcher功能。

按钮的代码如下:

self.button = tk.Button(self, text="Check IMEI", command=self.on_button)

而它应该执行的功能是:

def on_button(self):
self.button = tk.Button(self.root,command=searcher)

这意味着在按下按钮时,您将使用不同的命令(searcher)创建一个新按钮,并将其分配给原始按钮。当你第一次点击你的按钮时,就会发生这种情况;第二次单击它,"new"按钮将查找不存在的searcher函数——它不在类的范围内。

我的解决方案是

  • ImeiApp类中包含searcher函数
  • 完全放弃按钮功能
  • 指定self.searcher作为按钮的命令

开始:

class ImeiApp(tk.Tk):
def __init__(self):
self.root = tk.Tk()
self.entry = tk.Entry(self.root)
self.button = tk.Button(
self.root, text="Check IMEI", command=self.on_button)
self.button.pack()
self.entry.pack()
self.root.mainloop()
def on_button(self):
self.button = tk.Button(self.root, command=searcher)
def searcher(self):
...

请注意,我对类的__init__函数进行了一些更改。我没有使用类本身作为主窗口,而是生成了一个名为root的主窗口,并将按钮和入口小部件放在其中。

关于您的代码:正如jcfollower在评论中指出的那样,您应该查看effbot对tkinter的介绍,也许还可以查看tkinter上的Python文档,以了解有关如何使用tkinter的更多信息。

最新更新