Tkinter,图像消失和“按钮”问题不捕获点击

发布于 2025-01-12 04:49:15 字数 2000 浏览 1 评论 0原文

可能这是两个不同的问题,但我找不到任何一个的帮助,

我正在使用 TKINTER 做一个基本的界面,它可以在 PC 和带 LCD 屏幕的树莓派上运行,有很多行可以提出这个问题。界面很基本,只是顶部有一些按钮的图像,按钮是用形状制成的(即创建矩形)

首先出现的问题是背景图像仅短暂显示并消失,但在调试模式下则不然(Visual Studio代码和F5逐步步骤),其次,如果我不添加按钮背景,按钮不会捕获鼠标单击,如果添加边框(如下例所示),则只会在边框上被击中。

codenipet:

import tkinter as tk

...

self.canvas = tk.Canvas(self, bg=self.backgroundColor, bd=0, highlightthickness=0, relief='ridge')
self.canvas.config(cursor="circle")
self.canvas.pack(fill=tk.BOTH, expand=1)


self.canvas.bind("<Key>", event_keypress)
    for i_v in range(2):
        for i_h in range (5):
            tagName = "pad_button_" + str(i_v * 5 + i_h)
            self.canvas.tag_bind(tagName,"<Button-1>", lambda event, arg=tagName: event_callback(event, arg) )


self.canvas.create_image(10,10, image = myImg, anchor = "nw")

sleep(1)

keysize = 10
buttonsWidth = 2
colorOutline = 'red'
color = ''  # <- 

for i_v in range (0,2):
    for i_h in range (0,5):
        
        v_position = i_v * (keysize)
        h_position = i_h * (keysize)

        tagName = "pad_button_" + str(i_v * 5 + i_h)
        
        self.canvas.create_rectangle( h_position 
                                    , v_position 
                                    , h_position + keysize 
                                    , v_position + keysize 
                                    , width=buttonsWidth
                                    , fill=color
                                    , outline=colorOutline
                                    , tags=["pad_button",tagName] )

self.canvas.update()

我知道我不是很好,我正在学习,我被这个问题困扰了好几天,寻求帮助一直无法挺身而出

我测试了“in_function_scope”,似乎不是那么回事disvanice 也在:(

myImg = ...

img1=tk_canvas.create_image(4,4, image = myImg, anchor = "nw")

tk_canvas.pack()
tk_canvas.update()

time.sleep(0.3)

myImg = ...
img2=tk_canvas.create_image(40,40, image = myImg, anchor = "nw")

tk_canvas.pack()
tk_canvas.update()

不确定我在用这个包做什么,因为它没有任何效果)

probalby those are two separate issues, but i can not find help for any of them

I am doing an basic interface with TKINTER that work both on PC and raspberry with LCD screen, plenty of lines to raise this. Interface is basic, just a image with some bottons on top, buttons are made with shapes (i.e. create rectangle)

issue come first that background image is only displayed briefly and the disapear, but not on debuggin mode (Visual Studio code and F5 step by step), and second, buttons do not capture click of mouse if I do not add button backgroud, if added border (as bellow example), only get hitted on the border.

codesnipet:

import tkinter as tk

...

self.canvas = tk.Canvas(self, bg=self.backgroundColor, bd=0, highlightthickness=0, relief='ridge')
self.canvas.config(cursor="circle")
self.canvas.pack(fill=tk.BOTH, expand=1)


self.canvas.bind("<Key>", event_keypress)
    for i_v in range(2):
        for i_h in range (5):
            tagName = "pad_button_" + str(i_v * 5 + i_h)
            self.canvas.tag_bind(tagName,"<Button-1>", lambda event, arg=tagName: event_callback(event, arg) )


self.canvas.create_image(10,10, image = myImg, anchor = "nw")

sleep(1)

keysize = 10
buttonsWidth = 2
colorOutline = 'red'
color = ''  # <- 

for i_v in range (0,2):
    for i_h in range (0,5):
        
        v_position = i_v * (keysize)
        h_position = i_h * (keysize)

        tagName = "pad_button_" + str(i_v * 5 + i_h)
        
        self.canvas.create_rectangle( h_position 
                                    , v_position 
                                    , h_position + keysize 
                                    , v_position + keysize 
                                    , width=buttonsWidth
                                    , fill=color
                                    , outline=colorOutline
                                    , tags=["pad_button",tagName] )

self.canvas.update()

I know I am not very good, I am learning, and I got stuck with that for several days, looking help haven't been able to come forward

I tested the "in_function_scope", does not seem to be the thing as it disvanice also in:

myImg = ...

img1=tk_canvas.create_image(4,4, image = myImg, anchor = "nw")

tk_canvas.pack()
tk_canvas.update()

time.sleep(0.3)

myImg = ...
img2=tk_canvas.create_image(40,40, image = myImg, anchor = "nw")

tk_canvas.pack()
tk_canvas.update()

(not sure what I am doing with the pack as it does not have any effect)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文