如何创建一个Tkinter的画布按钮? [英] How do you create a Button on a tkinter Canvas?

查看:388
本文介绍了如何创建一个Tkinter的画布按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个框架,然后一个Canvas。结果
我想接下来要做的就是在画布上添加一个按钮。结果
然而,当我收拾按钮,我不能看到画布!

下面是我的尝试:

 从Tkinter的进口TK,画布,框,按钮
从Tkinter的导入这两个,W,NW,凹陷,TOP,X,FLAT,LEFT类示例(帧):
    高清__init __(自我,家长)​​:
        框架.__的init __(自我,父母)
        self.parent =父
        self.initUI()    高清initUI(个体经营):
        self.parent.title(布局测试)
        self.config(BG ='#F0F0F0)
        self.pack(补= BOTH,扩大= 1)
                #创建帆布
        canvas1 =帆布(个体经营,减免= FLAT,背景=#D2D2D2
                                            宽度= 180,高度= 500)
        canvas1.pack(边= TOP,锚= NW,padx = 10,pady = 10)
        #将退出按钮
        按钮1 =按钮(canvas1,文本=退出命令= self.quit,
                                                            锚= W)
        button1.configure(宽度= 10,activebackground =#33B5E5,
                                                        救援= FLAT)
        button1.pack(侧= TOP)高清的main():
    根= TK()
    root.geometry('800×600 + 10 + 50')
    应用实例=(根)
    app.mainloop()如果__name__ =='__main__':
    主要()


解决方案

在Tkinter的经理试图父控件调整到正确的尺寸,以遏制其子部件,和无大,默认情况下。所以帆布是有 - 但它precisely大小相同的按钮,并且因此不可见

如果你想放置在画布的一个小部件,而不的引起帆布带动态调整,你想要的 Canvas.create_window()功能

 #...略...
按钮1 =按钮(个体经营,文本=退出命令= self.quit,锚= W)
button1.configure(宽= 10,activebackground =#33B5E5,浮雕= FLAT)
button1_window = canvas1.create_window(10,10,锚= NW,窗口=按钮1)

这将在(10,10)相对于画布创建左上角的按钮,无需调整画布本身。

请注意,你可以与任何其他的Tkinter窗口部件的参考替换窗口参数。一个警告,虽然:命名部件必须是包含画布顶层窗口的孩子,或者位于同一顶层窗口小部件的一些的子

I created a Frame and then a Canvas.
What I want to do next is to add a Button on the Canvas.
However, when I packed the Button I cannot see the Canvas!

Here is what I tried:

from Tkinter import Tk, Canvas, Frame, Button
from Tkinter import BOTH, W, NW, SUNKEN, TOP, X, FLAT, LEFT

class Example(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Layout Test")
        self.config(bg = '#F0F0F0')
        self.pack(fill = BOTH, expand = 1)
                #create canvas
        canvas1 = Canvas(self, relief = FLAT, background = "#D2D2D2",
                                            width = 180, height = 500)
        canvas1.pack(side = TOP, anchor = NW, padx = 10, pady = 10)
        #add quit button
        button1 = Button(canvas1, text = "Quit", command = self.quit,
                                                            anchor = W)
        button1.configure(width = 10, activebackground = "#33B5E5",
                                                        relief = FLAT)
        button1.pack(side = TOP)

def main():
    root = Tk()
    root.geometry('800x600+10+50')
    app = Example(root)
    app.mainloop()

if __name__ == '__main__':
    main()

解决方案

The Tkinter pack manager tries to resize the parent widget to the correct size to contain its child widgets, and no larger, by default. So the canvas is there - but it's precisely the same size as the button, and thus invisible.

If you want to place a widget on a canvas without causing the canvas to dynamically resize, you want the Canvas.create_window() function:

# ... snip ...
button1 = Button(self, text = "Quit", command = self.quit, anchor = W)
button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
button1_window = canvas1.create_window(10, 10, anchor=NW, window=button1)

This will create your button with upper-left corner at (10, 10) relative to the canvas, without resizing the canvas itself.

Note that you could replace the window argument with a reference to any other Tkinter widget. One caveat, though: the named widget must be a child of the top-level window that contains the canvas, or a child of some widget located in the same top-level window.

这篇关于如何创建一个Tkinter的画布按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆