Python 在程序中创建Tkinter框时出现问题
Python 在程序中创建Tkinter框时出现问题,python,tkinter,Python,Tkinter,frame=frame(根,宽度=300,高度=250)未正确创建Tkinter窗口。你能找到问题并告诉我是什么吗。如果有帮助的话,我有剩下的代码 def clicking_pad(worker_1): root = Tk() global left_click def left_click(store: object) -> object: return store global right_click def right_
frame=frame(根,宽度=300,高度=250)
未正确创建Tkinter窗口。你能找到问题并告诉我是什么吗。如果有帮助的话,我有剩下的代码
def clicking_pad(worker_1):
root = Tk()
global left_click
def left_click(store: object) -> object:
return store
global right_click
def right_click(store: object) -> object:
return store
frame = Frame(root, width=300, height=250)
frame.bind("<Button-1>", left_click("GO"))
frame.bind("<Button-2>", right_click("STOP"))
frame.pack()
root.mainloop()
return right_click == right_click(store)
def点击键盘(工作区1):
root=Tk()
全局左键单击
def左键单击(存储:对象)->对象:
退货商店
全局右键单击
def右键单击(存储:对象)->对象:
退货商店
框架=框架(根,宽度=300,高度=250)
frame.bind(“,左键单击(“GO”))
frame.bind(“,右键单击(“停止”))
frame.pack()
root.mainloop()
返回右键单击==右键单击(存储)
正如布莱恩·奥克利(Bryan Oakley)所说,您的代码可以毫无问题地创建框架。
我也不知道你说的“没有正确创建Tkinter窗口”是什么意思
正如acw1668所说,您在代码中有一些错误:
变量和函数不能使用相同的名称-左键单击
,右键单击
bind()
(类似于command=
和after()
)需要函数名-这意味着没有()
。它被称为“回调”。如果需要使用带参数的函数,则可以创建不带参数的函数,该函数将带参数运行函数。您还可以使用lambda
直接在bind()中创建此函数
bind()
我的观点是:
是中间按钮,
是右按钮
bind()
或者如果函数返回值
def on_button_1(event):
global result
result = on_left_click("GO")
def on_button_3(event):
global result
result = on_right_click("STOP")
frame.bind("<Button-1>", on_button_1)
frame.bind("<Button-3>", on_button_3)
def on_按钮_1(事件):
全球结果
结果=左键单击(“GO”)
def on_按钮_3(事件):
全球结果
结果=右键单击(“停止”)
frame.bind(“,位于按钮1上)
frame.bind(“,位于按钮3上)
你所说的不正确是什么意思?你的代码的主要问题是:1)你用函数覆盖左键单击
和右键单击
;2) 将函数的结果而不是对函数的引用传递给frame.bind(…)
;3) 无法从bind(…)
中使用的函数中获取返回值。为什么说它没有创建帧?当我运行代码时,框架会像我期望的那样被创建。
是中间按钮,
是右按钮。
def on_button_1(event):
on_left_click("GO")
def on_button_3(event):
on_right_click("STOP")
frame.bind("<Button-1>", on_button_1)
frame.bind("<Button-3>", on_button_3)
def on_button_1(event):
global result
result = on_left_click("GO")
def on_button_3(event):
global result
result = on_right_click("STOP")
frame.bind("<Button-1>", on_button_1)
frame.bind("<Button-3>", on_button_3)