簡體   English   中英

由於 window 也在 tkinter GUI 中調整大小

[英]As the window resizes resize everytinhg in tkinter GUI as well

所以我做了這個簡單的程序。 問題是我事先沒有想到,當 window 調整大小時,我希望其中的所有對象也調整大小。 有沒有辦法寫一些簡單的東西來影響下面的一切,而不是寫一些東西到每一行? 為了讓事情更清楚,您可以運行我的代碼並調整 window 的大小。 你會明白問題出在哪里:

from tkinter import * 
import sys
from PIL import ImageTk, Image
import math
import tkinter.font as TkFont

root = tk.Tk()


#Lango dydis
#root.geometry("500x600+0+0")
HEIGHT = 500
WIDTH = 600

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH )
canvas.pack()

frame=Frame(root, bg='#80c1ff', bd=5)
frame.place(relwidth=1, relheight=1)

trikampis = ImageTk.PhotoImage(Image.open("E:/Pytono projektai/240px-Rtriangle.png"))
trikampis2 = Label(root, image=trikampis)
trikampis2.place(relx= 0.29, rely= 0.2)


def skaiciuotuvas():
    error = Label(root, width=1, bg='#80c1ff', fg='#80c1ff')
    error.place(relwidth=0.8, relheight=0.04, relx= 0.12, rely= 0.905)
    a1 = ae.get()
    b1 = be.get()
    c1 = ce.get()
    if a1 == '':
        b11 = float(b1)
        c11 = float(c1)
        a11 = float(c11)**2 - float(b11)**2
        a12 = math.sqrt(a11)
        a13 = '{:.5}'.format(a12)
        ae.insert(0, a13)
        a111 = '{:.5}'.format(a11)
        saknies.config(text=a111)
        a20 = '{:.10}'.format(a12)
        galutinis.config(text=a20)
        b20 = b11
        c20 = c11
    if b1 == '':
        a11 = float(a1)
        c11 = float(c1)
        b11 = float(c11)**2 - float(a11)**2
        b12 = math.sqrt(b11)
        b13 = '{:.5}'.format(b12)
        be.insert(0, b13)
        b111 = '{:.5}'.format(b11)
        saknies.config(text=b111)
        b20 = '{:.10}'.format(b12)
        galutinis.config(text=b20)
        a20 = a11
        c20 = c11
    if c1 == '':
        a11 = float(a1)
        b11 = float(b1)
        c11 = float(a11)**2 + float(b11)**2
        c12 = math.sqrt(c11)
        c13 = '{:.5}'.format(c12)
        ce.insert(0, c13)
        c111 = '{:.5}'.format(c11)
        saknies.config(text=c111)
        c20 = '{:.10}'.format(c12)
        galutinis.config(text=c20)
        a20 = a11
        b20 = b11
    elif (a1 != '') and (b1 != '') and (c1 != ''):
        error = Label(root, width=1, text='BENT VIENA REIKŠMĖ TURI BŪTI NEŽINOMA!', font='Verdana 12 bold', bg='white', fg='red')
        error.place(relwidth=0.8, relheight=0.04, relx= 0.12, rely= 0.905)
    ploto1 = float(a20)
    ploto2 = float(b20)
    plotass = (ploto1*ploto2)/2
    plotassg = '{:.10}'.format(plotass)
    plotoats.config(text=plotassg)
    perimetro1 = float(a20)
    perimetro2 = float(b20)
    perimetro3 = float(c20)
    perimetrof = perimetro1 + perimetro2 + perimetro3
    perimetroff = '{:.9}'.format(perimetrof)
    perimetroats.config(text=perimetroff)

def resetas():
    error = Label(root, width=1, bg='#80c1ff', fg='#80c1ff')
    error.place(relwidth=0.8, relheight=0.04, relx= 0.12, rely= 0.905)
    ae.delete(0, END)
    be.delete(0, END)
    ce.delete(0, END)
    saknies.config(text='')
    galutinis.config(text='')
    plotoats.config(text='')
    perimetroats.config(text='')


plotoats = Label(root, width=1, font='Verdana 12 bold', bg='white', text='')
plotoats.place(relwidth=0.2, relheight=0.04, relx= 0.55, rely= 0.85)

plotas = Label(root, width=1, font='Verdana 12 bold', bg='white', text='Trikampio plotas = ')
plotas.place(relwidth=0.3, relheight=0.04, relx= 0.24, rely= 0.85)

plotoats = Label(root, width=1, font='Verdana 12 bold', bg='white', text='')
plotoats.place(relwidth=0.2, relheight=0.04, relx= 0.55, rely= 0.85)



perimetras = Label(root, width=1, font='Verdana 12 bold', bg='white', text='Trikampio perimetras = ')
perimetras.place(relwidth=0.37, relheight=0.04, relx= 0.21, rely= 0.795)

perimetroats = Label(root, width=1, font='Verdana 12 bold', bg='white', text='')
perimetroats.place(relwidth=0.19, relheight=0.04, relx= 0.59, rely= 0.795)






ats = Label (root, width=1, font='Verdana 12 bold', bg='white', text='Ats.: ')
ats.place(relwidth=0.1, relheight=0.04, relx= 0.21, rely= 0.74)


im = Image.open("E:/Pytono projektai/square-root-symbol-png-3.png")
new_size = im.resize((36, 16))
ph = ImageTk.PhotoImage(new_size)
label = Label(image=ph)
label.place(relx= 0.32, rely= 0.74)

saknies = Label(root, width=1, font='Verdana 12 bold', bg='white', text='')
saknies.place(relwidth=0.1, relheight=0.04, relx= 0.40, rely= 0.74)

lygu = Label(root, width=1, font='Verdana 12 bold', bg='white', text='=')
lygu.place(relwidth=0.05, relheight=0.04, relx= 0.515, rely= 0.74)

galutinis = Label(root, width=1, font='Verdana 12 bold', bg='white', text='')
galutinis.place(relwidth=0.2, relheight=0.04, relx= 0.58, rely= 0.74)

skaiciuoti = Button(text='SKAIČIUOTI', font='Verdana 12 bold', command=skaiciuotuvas)
skaiciuoti.place(relwidth=0.4, relheight=0.04, relx= 0.29, rely= 0.62)

reset = Button(text='RESET', font='Verdana 12 bold', command=resetas)
reset.place(relwidth=0.4, relheight=0.04, relx= 0.29, rely= 0.68)

a = Label(root, width=1, text='a=', font='Verdana 12 bold', bg='white')
a.place(relwidth=0.04, relheight=0.04, relx= 0.18, rely= 0.38)

ae = Entry(root, width=1, font=10, border=2)
ae.place(relwidth=0.09, relheight=0.04, relx= 0.23, rely= 0.38)

b = Label(root, width=1, text='b=', font='Verdana 12 bold', bg='white')
b.place(relwidth=0.04, relheight=0.04, relx= 0.38, rely= 0.562)

be = Entry(root, width=1, font=10, border=2)
be.place(relwidth=0.09, relheight=0.04, relx= 0.43, rely= 0.562)

c = Label(root, width=1, text='c=', font='Verdana 12 bold', bg='white')
c.place(relwidth=0.04, relheight=0.04, relx= 0.50, rely= 0.37)

ce = Entry(root, width=1, font=10, border=2,)
ce.place(relwidth=0.09, relheight=0.04, relx= 0.545, rely= 0.37)

root.mainloop()```


我認為解決您的問題的唯一方法是將您使用的每個.place方法更改為.grid.pack 在我看來,使用上述方法比使用place更簡單、更快捷。

Place 方法允許您設置相對或絕對 position,實際上您正在這樣做。 當你想讓你的小部件根據它的父級改變大小/位置時,它很有用。 如果這不是您的意圖,您應該使用gridpack

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM