調用每當我Tkinter
程序蟒蛇的東西,代碼如下所示:「未定義」的功能參數在Python
from Tkinter import *
class GUI():
def __init__(self, master):
self.master = master # master is an instance of Tk
self.master.title("") # set the name of the window
self.frame = Frame(self.master, width=800, height=500, bg="#eeeeee")
# 800, 500 and "#eeeeee" are examples of course
self.frame.pack()
self.canvas = Canvas(self.frame, width=800, height=500, bg="ffffff")
self.canvas.place(x=0, y=0)
#mostly there are some other widgets
# here are obviously other methods
def main():
root = Tk()
app = GUI(root) # root and app.master are synonyms now
app.master.mainloop()
if __name__ == '__main__':
main()
我的問題是,我真的不明白,爲什麼Frame(width=800, height=500)
或place(x=0, y=0)
工作:我沒有定義參數height
,width
,x
和y
。查看模塊Tkinter
中的代碼,函數需要一個名爲*args
或**kw
的參數。我知道如何使用它們(至少足夠開發一些小應用程序),但我不知道如何定義一個使用這個參數的函數。我覺得我對python的這一部分並不十分了解,儘管我可以使用它。
所以我的問題是:
我怎麼可以定義一個函數,所謂的語法如下:
functionName(parameterName1 = value, paramterName2 = value, ...)
我並不需要知道如何使一個功能是什麼接受變參數的數量(與我的問題相結合),但它也可以。
任何基本的python教程都會告訴你如何做到這一點。這是標準教程中的[定義函數](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)。 – tdelaney