【发布时间】:2016-11-18 16:52:06
【问题描述】:
每当我在 python 中使用Tkinter 编程时,代码如下所示:
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 教程都会向您展示如何做到这一点。这是标准教程中的Defining Functions。
标签: python python-2.7 function syntax tkinter