zoukankan      html  css  js  c++  java
  • tkinter使用pack和grid实现缩放布局

    pack

    关键是理解side,anchor,expand,fill的使用。

    有一篇文章写的不错,但是要耐心看,反复看,不容易理解。

    https://blog.csdn.net/superfanstoprogram/article/details/83713196

    import tkinter as tk
    import tkinter.ttk as ttk
    
    win=tk.Tk()
    win.title("CommunicationTool")
    #setting
    setFrame = tk.LabelFrame(win,text="Setting")
    comFrame = tk.Frame(setFrame)
    comLable = tk.Label(comFrame,text="COM Port: ").pack(side=tk.LEFT)
    comSpiner = tk.Spinbox(comFrame,text="COM1").pack(side=tk.LEFT)
    refrashButton = ttk.Button(comFrame,text="Refresh").pack(side=tk.LEFT)
    comFrame.pack(anchor=tk.W)
    cmdFrame = tk.Frame(setFrame)
    inputLable = tk.Label(cmdFrame,text="Command: ").pack(side=tk.LEFT)
    inputEntry = tk.Entry(cmdFrame).pack(side=tk.LEFT,expand=1,fill=tk.X)
    sendButton = ttk.Button(cmdFrame,text="Send").pack(side=tk.LEFT)
    cmdFrame.pack(anchor=tk.W,expand=1,fill=tk.X)
    setFrame.pack(fill=tk.BOTH)
    #output
    outputFrame = tk.LabelFrame(win,text="Output")
    area = tk.Text(outputFrame).pack(expand=1,fill=tk.BOTH)
    outputFrame.pack(expand=1,fill=tk.BOTH)
    
    win.mainloop()
      
    

      

     grid

    默认情况下,grid中的部件不缩放。

    缩放的关键:

    rowconfigure()
    columnconfigure()

    示例代码:
    import tkinter as tk
    import tkinter.ttk as ttk

    win=tk.Tk()
    win.title("CommunicationTool")
    win.rowconfigure(1, weight=1)
    win.columnconfigure(0, weight=1)
    #setting
    setFrame = tk.LabelFrame(win,text="Setting")
    setFrame.columnconfigure(2, weight=1)
    comLable = tk.Label(setFrame,text="COM Port: ").grid(row=0,column=0)
    comSpiner = tk.Spinbox(setFrame,text="COM1").grid(row=0,column=1,sticky=tk.EW)
    refrashButton = ttk.Button(setFrame,text="Refresh").grid(row=0,column=2,sticky=tk.W)
    inputLable = tk.Label(setFrame,text="Command: ").grid(row=1,column=0)
    inputEntry = tk.Entry(setFrame).grid(row=1,column=1,sticky=tk.EW,columnspan=2)
    sendButton = ttk.Button(setFrame,text="Send").grid(row=1,column=3)
    setFrame.grid(row=0,sticky=tk.EW)
    #output
    outputFrame = tk.LabelFrame(win,text="Output")
    outputFrame.rowconfigure(0,weight=1)
    outputFrame.columnconfigure(0,weight=1)
    area = tk.Text(outputFrame).grid(row=0,sticky=tk.NSEW)
    outputFrame.grid(row=1,sticky=tk.NSEW)

    win.mainloop()

    默认效果:

    最大化效果:

  • 相关阅读:
    Protected和Default的区别
    将数组中负数放在正数前面
    java.io包和杯子测楼
    hadoop基础
    极限编程和JUnit
    接口和抽象类
    C# 中窗口AutoScaleMode属性
    计算机的自启动管理
    labview中的移位寄存器、循环隧道,自动索引隧道的区别
    发现C#winform编程中不常用的控件(一)<FlowLayoutPanel控件><拆分器控件Splitcontainer >
  • 原文地址:https://www.cnblogs.com/real-bert/p/11569198.html
Copyright © 2011-2022 走看看