【发布时间】:2017-06-10 22:25:57
【问题描述】:
我试图让我的保存按钮调用另一个类的函数。我想尽可能多地点击保存按钮,它应该每次都打印“hello people”。不过,我无法让保存按钮正常工作。
import tkinter as tk
from tkinter import filedialog
class Application(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.pack()
self.createWidgets()
def createWidgets(self):
#save button
self.saveLabel = tk.Label(self.parent, text="Save File", padx=10, pady=10)
self.saveLabel.pack()
#When I click the button save, I would like it to call the test function in the documentMaker class
self.saveButton = tk.Button(self.parent, text = "Save", command = documentMaker.test(self))
self.saveButton.pack()
class documentMaker():
def test(self):
print ("hello people")
root = tk.Tk()
app = Application(root)
app.master.title('Sample application')
object = documentMaker()
object.test()
app.mainloop()
【问题讨论】:
-
查看我不久前回答的这个问题stackoverflow.com/questions/44012740/…
-
尝试google你的标题。有很多结果可以回答这个问题。这种问题在google和stackoverflow上很容易找到答案。
标签: python-2.7 python-3.x tkinter