Python 如何从导入的包调用tkinter中的方法?
我有一个记录屏幕的python脚本,但现在当我尝试使用tkinter运行屏幕记录脚本并从tk窗口中的按钮终止时,我面临以下问题: self.recorderButton=按钮(frame,text=“Start Recording”,command=self.Recorder.record\u屏幕)AttributeError:“试用”对象 没有“记录器”属性 这是我的python代码:Python 如何从导入的包调用tkinter中的方法?,python,user-interface,tkinter,Python,User Interface,Tkinter,我有一个记录屏幕的python脚本,但现在当我尝试使用tkinter运行屏幕记录脚本并从tk窗口中的按钮终止时,我面临以下问题: self.recorderButton=按钮(frame,text=“Start Recording”,command=self.Recorder.record\u屏幕)AttributeError:“试用”对象 没有“记录器”属性 这是我的python代码: from tkinter import * import Recorder class Trial:
from tkinter import *
import Recorder
class Trial:
def __init__(self, master):
frame = Frame(master)
frame.pack()
# self.printButton = Button(frame, text="Print Message", command=self.printMessage)
# self.printButton.pack(side=LEFT)
self.recorderButton = Button(frame, text="Start Recording", command=self.Recorder.record_screen)
self.recorderButton.pack(side=LEFT)
self.quitButton = Button(frame, text="Quit", command=frame.quit)
self.quitButton.pack(side=LEFT)
def printMessage(self):
print("Hello there its me Nitin!")
root = Tk()
alpha = Trial(root)
root.mainloop()
record\u screen()
是属于Recorder
模块的函数。因此,用self作为前缀是没有意义的
我的意思是,您应该按照以下方式编写回调:
self.recorderButton = Button(frame, text="Start Recording", command=Recorder.record_screen)
编辑:
from tkinter import *
import Recorder
class Trial:
def __init__(self, master):
frame = Frame(master)
frame.pack()
# self.printButton = Button(frame, text="Print Message", command=self.printMessage)
# self.printButton.pack(side=LEFT)
self.recorderButton = Button(frame, text="Start Recording", command=self.Recorder.record_screen)
self.recorderButton.pack(side=LEFT)
self.quitButton = Button(frame, text="Quit", command=frame.quit)
self.quitButton.pack(side=LEFT)
def printMessage(self):
print("Hello there its me Nitin!")
root = Tk()
alpha = Trial(root)
root.mainloop()
通过你的评论来回答这个问题
当您为按钮command=something
编写此选项时,该something
必须是回调(函数),我猜您没有在某个地方实现该回调
您可以在类中添加此函数:
def quit(self):
self.frame.destroy()
和将(
…command=self.frame.quit
)更改为(…command=self.quit
)@Bilal BEGUERADJ我尝试了你的答案,程序现在运行,但当我单击“退出”按钮时它不会终止。我确实这样做了,但代码仍然没有终止