【问题标题】:Python3 tkinter canvas, setting relative width and heightPython3 tkinter 画布,设置相对宽度和高度
【发布时间】:2020-08-30 08:53:59
【问题描述】:

我正在尝试将某些画布的宽度设置为母版的宽度(全屏),将高度设置为母版的 1/5、3/10 和 1/2。窗口正确显示,但画布没有出现。我尝试使用 relwidth = 1 和 relheight = 0.2 的 place() 而不是 pack() 失败。 我只想让我的三个画布堆叠并水平填充窗口。

master = Tk()
master.attributes('-zoomed', True)

w = master.winfo_width()
h = master.winfo_height()
hu = int(h/10)
h1 = hu*2
h2 = hu*3
h3 = hu*5

c1 = Canvas(master, bg='grey', width = w, height = h1)
c1.pack()
c2 = Canvas(master, bg='blue', width = w, height = h2)
c2.pack()
c3 = Canvas(master, bg='red', width = w, height = h3)
c3.pack()

编辑:我的工作代码现在是:

c1 = Canvas(master)
c1.place(rely = 0, relheight = 0.2, relwidth = 1)
c2 = Canvas(master)
c2.place(rely = 0.2, relheight = 0.3, relwidth = 1)
c3 = Canvas(master)
c3.place(rely = 0.5, relheight = 0.5, relwidth = 1)

但我有一个副作用,我无法在中心对齐文本:

c1.create_text(0, 0, text = 'text', fill = 'red', font = ('olivier', 30, 'bold'))
c1.update()

我以这种不雅的方式解决了:

master.update_idletasks()
w = master.winfo_width()
h = master.winfo_height()
ww = int(w/2)
hh = int(h/10)
c1.create_text(ww, hh, text = 'text', fill = 'red')

【问题讨论】:

    标签: python python-3.x tkinter tkinter-canvas


    【解决方案1】:

    您需要使用.place() 几何管理器。

    .place() 有两个我认为你需要的主要参数。

    第一个是relheight。正如它的名字一样,它设置了任何小部件相对于它的父小部件的高度。

    因此,如果 relheight 为 0.5,则高度将为 0.5(高度),即父窗口小部件高度的 50%。

    这同样适用于relwidth,它的工作方式相同,但适用于宽度。

    阅读更多关于 .place() here 的信息。

    另外,如果你想要你的代码:

    master = Tk()
    master.attributes('-zoomed', True)
    
    c1 = Canvas(master, bg='grey')
    c1.place(x = 0, rely = 0, relheight = 0.2, relwidth = 1)
    c2 = Canvas(master, bg='blue')
    c2.pack(x = 0, rely = 0.25, relheight = 0.3, relwidth = 1)
    c3 = Canvas(master, bg='red')
    c3.pack(x = 0, rely = 0.5, relheight = 0.5, relwidth = 1)
    

    希望这会有所帮助!

    编辑:.place() 不适用于画布元素。为此,您需要取画布的长度和宽度,然后将两者除以二(如果您想要中间)。

    例如:

    height = 100
    width = 100
    c = Canvas(master, bg = "blue", height = height, width = width, highlighthickness = 0)
    c.place(x = 0, rely = 0.5, relheight = 0.2, relwidth = 1)
    text = c.create_text(height/2, width/2, text = "text")
    

    这会将文本text 放置在画布的中心。

    希望这会有所帮助!

    【讨论】:

    • 您还需要指定xrely
    • 应该是x=0,因为 OP 希望画布水平填充窗口。 rely 也应分别为 00.20.5
    • @acw1668 已编辑。
    • 如果成功了,你介意点赞吗?谢谢! @druido82
    • 已接受答案,但我有副作用:我无法在画布中对齐 create_text 的中心...我更新了我的问题,“x = 0”已删除,它没有效果跨度>
    【解决方案2】:

    第一次使用master.winfo_width()master.winfo_height(),都是零。(如果你使用print(w,h),你会发现它们都是零。)

    只需在master.winfo_xxxx 之前添加master.update_idletasks()

    所有代码都可以是:

    from tkinter import *
    master = Tk()
    master.state('zoomed')
    
    # add here 
    master.update_idletasks()
    w = master.winfo_width()
    h = master.winfo_height()
    hu = int(h/10)
    h1 = hu*2
    h2 = hu*3
    h3 = hu*5
    
    c1 = Canvas(master, bg='grey', width = w, height = h1)
    c1.pack()
    c2 = Canvas(master, bg='blue', width = w, height = h2)
    c2.pack()
    c3 = Canvas(master, bg='red', width = w, height = h3)
    c3.pack()
    master.mainloop()
    

    【讨论】:

    • 工作!我接受了以前的答案,无论如何谢谢你,现在我明白我错在哪里了......
    猜你喜欢
    • 1970-01-01
    • 2014-06-15
    • 2021-05-08
    • 2020-03-29
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2014-10-30
    相关资源
    最近更新 更多