import tkinter as tk
from tkinter import ttk
def draw_mine_field(x=5, y=3):
mine_frame = tk.Toplevel(root)
mine_frame.grid()
root.withdraw()
for a in range(x):
for b in range(y):
ttk.Button(mine_frame, text='*', width=3).grid(column=a, row=b)
root = tk.Tk()
root.title("MS")
startframe = ttk.Frame(root)
ttk.Label(root,text="y").grid(row=1,column=1)
y_entry_box = ttk.Entry(root).grid(row=1,column=2)
ttk.Label(root,text="x").grid(row=1,column=3)
x_entry_box = ttk.Entry(root).grid(row=1,column=4)
ttk.Button(root,text="Start",command=draw_mine_field).grid(row=2,column=1)
ttk.Button(root,text="Quit",command=root.destroy).grid(row=2,column=2)
root.mainloop()
对于此特定示例可能有一种更简单的方法。基本上,我想知道的是在command=draw_mine_field
中传递参数时,如何在不运行该函数的情况下通过(x, y)
?总的来说,这是如何工作的?通过函数参考时传递参数
x和y来自哪里,如果不是默认值? –
您不能将值传递给函数而不运行它。在这个设计中你可以做的是设置一些全局变量,并且使用这些函数而不是参数,但这并不完全是优雅的设计。通常在GUI中,您需要将x和y值输入到输入窗口小部件(文本框或旋转框)中,并从中读取绘图函数。 –