使用Tkiners Button命令访问课堂外的类方法



我遇到了一个问题,试图从tkiners按钮上的命令调用类的方法。我尝试了:

command = Alarm_clock.save_alarm()
command = self.save_alarm()
command = Alarm_clock.save_alarm(self.hour_count, self.min_count)
command = Alarm_clock.save_alarm(self)

我认为我缺少一些明显的东西,因为我设法在同一类中"命令"方法。

这是我的代码的片段:它是一个闹钟:

import tkinter as tk
import time
import vlc
import pygame
from mutagen.mp3 import MP3
class Alarm_clock():
    def __init__(self):
        alarm_list = []
    def save_alarm(self):
        print(init_alarm_gui.hour_count.get())
        print(init_alarm_gui.min_count.get())
        #get the hour and minute and append it to the alarm_list to be checked on real time
class init_alarm_gui():
    def __init__(self, root):
        self.root = root
        self.hour_count = 0
        self.min_count = 0
    def make_widgets(self, root):
        self.hour_count = tk.IntVar()
        self.min_count = tk.IntVar()
        self.time_label = tk.Label(text="")
        self.time_label.pack()
        self.Save_but = tk.Button(root, text = "Save Alarm", command=Alarm_clock.save_alarm)
        self.Save_but.pack()  
def main():
    root = tk.Tk()
    gui = init_alarm_gui(root)
    gui.make_widgets(root)    
    root.mainloop()   
if __name__ == "__main__":
    main()

 

TyperError: save_alarm() missing 1 required positional argument: 'self'

您必须创建类的实例,然后调用实例的方法。这将自动填充self参数。这不是Tkinter唯一的,而是Python的工作方式。

alarm = Alarm_clock()
alarm.save_alarm()

最新更新