【问题标题】:resize items window automatically python interface自动调整项目窗口大小python界面
【发布时间】:2021-06-13 21:26:51
【问题描述】:

在问之前,我要感谢所有 Stackoverflow 成员 我正在开发 GUI python,如下图所示 enter image description here

这也有效,当我最大化窗口时出现问题,如下图所示

enter image description here

我想使窗口项目自动调整大小

import sqlite3
from tkinter import *
from tkinter.ttk import Combobox
from PIL import Image, ImageTk



path = "C:\\Users\\Elkassah\\Desktop\\Python\\Test sqlite.db"
conn = sqlite3.connect(path)
def insertbd():
    
    part = int(Echantillons.get())
    sex=V0.get()
    age=int(age.get())
    sco=int(Puissance.get())
    
    fonct=cb.get()
    rev=int(Revenu.get())
    red=data2.get()
    rep = (part,sex,age,sco,fonct,rev,red)
    print(rep)
    sql = """insert into enquete (Echantillons, Sexe, Age, Puissance,Fonction, Revenue, Queston1 ) values (?,?,?,?,?,?,?)"""
    with conn as c :
        c.execute(sql,rep)
 
window=Tk()


#backgroud image
canvas = Canvas(window, width = 1500, height = 800)  
canvas.pack()  
img = ImageTk.PhotoImage(Image.open("qlt.png"))  
canvas.create_image(0, 0, anchor=NW, image=img) 
#logo image
canva = Canvas(window, width = 500, height = 500)  
canva.pack()  
imge = ImageTk.PhotoImage(Image.open("download.png"))  
canva.create_image(0, 0, anchor=NW, image=imge) 


lbEchantillons=Label(window,text="Echantillons des TV :")
lbEchantillons.place(x=60,y=100)
Echantillons = Entry(window)
Echantillons.place(x=200,y=100)

color=Label(window,text='Couleur d affichage')
color.place(x=60,y=150)

V0 = IntVar()
V0.set(1)

color1=Radiobutton(window, text="Blanc/Noir",variable=V0,value=1)
color1.place(x=200,y=150)

color2=Radiobutton(window,text="Encouleur",variable=V0,value=2)
color2.place(x=300,y=150)



lbPuissance=Label(window,text='Puissance electrique\n en miliWatt',)
lbPuissance.place(x=60,y=200)
Puissance = Entry(window)
Puissance.place(x=200,y=200)

lbTaille=Label(window,text='  Taille de l écran     \n en pouce ',fg='blue' ,bd=0)
lbTaille.place(x=60,y=250)
Taille = Entry(window)
Taille.place(x=200,y=250)

Satisfaction=Label(window,text='Satisfaction du cliente')
Satisfaction.place(x=60,y=350)

V1 = IntVar()
V1.set(1)

Satisfaction1=Radiobutton(window, text="Oui ",variable=V1,value=1)
Satisfaction1.place(x=200,y=350)

Satisfaction2=Radiobutton(window,text="Non ",variable=V1,value=2)
Satisfaction2.place(x=280,y=350)




ok =Button(window,text= "       Validé      ", command=insertbd )
ok.place(x=200,y=400)
window.title("TV report ")
window.geometry("450x470+10+10")

window.mainloop()

【问题讨论】:

  • 请专门提供一些代码minimal reproducible example,您尝试了什么?
  • 我建议阅读 packgrid 向窗口添加小部件的方法。它们使创建响应式 GUI 比 place 更容易。

标签: python python-3.x list python-2.7 tkinter


【解决方案1】:
    width = window.winfo_width()
    height = window.winfo_height()
    img_pil = Image.open(qlt.png)
    rsd_bg_img = img_pil.resize((width,height))
    rsd_bg = canvas.create_image(0,0,anchor = NW,image = rsd_bg_img)

    def on_resize():
        width = window.winfo_width()
        height = window.winfo_height()
        rsd_bg_img = img_pil.resize((width,height))
        canvas.itemconfig(rsd_bg,image = rsd_bg_img)
        #for other widgets, widget_current_width = widget_default_width * (window_current_width/window_default_width),replicated for height.

    window.bind("<Configure>", on_resize)

【讨论】:

  • bind() 的回调需要一个参数,即事件对象。
猜你喜欢
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 2017-04-29
  • 1970-01-01
  • 2011-02-28
  • 2011-08-18
相关资源
最近更新 更多