Python与tkinter长宽比问题

时间:2018-02-19 08:11:54

标签: python python-3.x tkinter

我正在尝试使用 3.6.4和 8.6创建一个在调整大小时保持方形的简单窗口。这是我生成窗口的代码,但在调整大小时不会保持其宽高比。

import tkinter as tk
w = tk.Tk()
w.aspect(1,1,1,1)
w.mainloop()

1 个答案:

答案 0 :(得分:0)

也许您可以使用画布来制作,他会检测事件(图像大小已更改)和.place相对x和相对y。我尝试制作一个脚本来帮助你,但你需要做一些改变

from tkinter import *
# create a canvas with no internal border
canvas = Canvas(bd=0, highlightthickness=0)
canvas.pack(fill=BOTH, expand=1)
lastw, lasth = canvas.winfo_width(), canvas.winfo_height()

# track changes to the canvas size and draw
# a rectangle which fills the visible part of
# the canvas

def configure(event):
    global lastw, lasth
    canvas.delete("all")
    w, h = event.width, event.height
    try:
        label.config(font = ('Arial ', int(12 * ((w - lastw) / (h - lasth))))) # -- this formula need change :3
    except ZeroDivisionError: pass
    lastw, lasth = canvas.winfo_width(), canvas.winfo_height()

canvas.bind("<Configure>", configure)
label = Label(canvas, text = "YOLO")
label.place(relx = 0.5, rely = 0.5) # - this make the widget automatic change her pos

mainloop()

您可以在此处查看此工作http://effbot.org/zone/tkinter-window-size.htm