如何在单个小部件中包装多个功能?
我想打包一些功能。在单个小部件中,我可以使用bind func与该特定小部件进行交互。
有框架小部件在其中包装了小部件和canvas.create_window func。在帆布小部件中,它也与框架相同。
以下程序在每3秒后生成Sticman。当用户单击“ Stickman”时,它会消失。
我尝试使用框架打包功能
I want to pack some func. in a single widget so that I can interact with that particular widget using bind func.
There's Frame widget which packs up widgets in it and canvas.create_window func. in Canvas widget which also does the same as Frame.
Following program generates sticman after every 3 sec. And when user click the stickman, it disappears.
I tried using Frame to pack functions ????...
from Tkinter import *
root = Tk()
hitbox = Frame(root, height = 100, width= 100)
hitbox.pack()
can = Canvas(hitbox, height= 450, width= 1000) # Canvas inside hitbox
can.pack()
def stickman (a,b,c,d):
# Code that makes stickman according to coordinates
def HP(p,q):
# Code that makes Progressbar widget inside hitbox which act as healthbar
counter = 0
def init():
if counter == 10:
pass
else:
counter +=1
stickman(100,100,130,130)
HP(90, 120)
root.after(3000, init) # Stickman respawns after 3 seconds
def kill():
hitbox.destroy()
hitbox.bind('<Button-1>', kill)
root.mainloop()
Stickman respawns after every 3 seconds but bind func. with frame does not seems to be working when running code. Stickman doesn't disappears when clicked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您要的是在按钮1或单击鼠标上的左键时销毁TKINTER框架。 Stickman不会消失,因为框架没有积极地聆听键单击,而与标签不同,或者入口字段会这样做。
因此,有两个简单的解决方案解决此问题:
1。绑定到根窗口
将钥匙绑定到根窗口的键窗口将解决该问题,主要是因为根窗口始终会积极地聆听键键,而与帧或输入字段不同。这种方法的唯一问题是用户可以单击窗口上的任何地方以破坏框架。
2。绑定到贴纸本身
将钥匙束绑定到Stickman本身是最干净的方法,因为它与第一个解决方案相同,但是这次用户只能单击Stickman即可破坏框架。这可能是您要寻找的解决方案。要实现此解决方案,只需替换
root.bind('&lt; button-1&gt;',kill)
用stickman.bind('&lt; button; button; button-1&gt;',kill)(或您的Stickman的名字是什么)。
我在下面的第一个选项上添加了您的代码的修改版本:
I think what you are asking for is to destroy the Tkinter frame when Button 1 or the left button on the mouse is clicked. The stickman doesn't disappear because the frame isn't actively listening for the key click unlike a label or an entry field would do.
So, there are 2 easy fixes for this problem:
1. Binding to Root Window
Binding the keybind to the root window would solve the problem mostly because the root window is always actively listening for keybinds unlike the frame or an input field. The only problem with this approach is that the user can click anywhere on the window to destroy the frame.
2. Binding to the Stickman Itself
Binding the keybind to the stickman itself is the cleanest approach because it would be just the same as the first solution but this time the user can only click on the stickman to destroy the frame. This is probably the solution you were looking for. To implement this solution just replace the
root.bind('<Button-1>', kill)
withstickman.bind('<Button-1>', kill)
(or whatever the name is of your stickman is) after defining the stickman but before packing it.I atatched a modified version of your code down below for the first option: