【问题标题】:How can I set the size of my Tkinter widgets in pixels?如何以像素为单位设置我的 Tkinter 小部件的大小?
【发布时间】:2020-02-16 13:29:05
【问题描述】:

每次我必须在 Python 中设计 GUI 时,我总是首选 PyQt5(Qt Designer 5),因为在我看来,它比 Tkinter 更容易,但现在我有一个任务,并且必须使用 Tkinter。

我想在 Tkinter 中实现的 GUI 是这样的:

Qt Designer 5 中的图形用户界面

这是我为 Tkinter GUI 设计所做的一段代码:

from tkinter import Tk,Text
from tkinter import ttk

class MiApp(ttk.Frame):
    def __init__(self,main_window):
        super().__init__(main_window)
        self.promDaily = []
        self.datos = {}
        self.csv = " "
        self.botonCSV = ttk.Button(main_window,text="Escoger CSV",width=91,command=self.ChooseCSVFile)
        self.botonCSV.place(x=9,y=16)
        self.pathCSV = ttk.Entry(main_window,width=351,state="disable")
        self.pathCSV.place(x=120,y=17)
        self.labelAbsc = ttk.Label(main_window,text="Abscisa:",width=47)
        self.labelAbsc.place(x=9,y=52)
        self.labelOrden = ttk.Label(main_window,text="Ordenada:",width=55)
        self.labelOrden.place(x=120,y=52)
        self.labelPromD = ttk.Label(main_window,text="Promedio diario:",width=79)
        self.labelPromD.place(x=245,y=52)
        self.comboBAbsc = ttk.Combobox(main_window,width=91)
        self.comboBAbsc.place(x=9,y=78)
        self.comboBOrden = ttk.Combobox(main_window, width=91)
        self.comboBOrden.place(x=120, y=78)
        self.botonCalc = ttk.Button(main_window,text="Calcular",width=75)
        self.botonCalc.place(x=245,y=78)
        self.posibilidadCalc = ttk.Entry(main_window,width=211)
        self.posibilidadCalc.place(x=245,y=114)
        self.botonGraph = ttk.Button(main_window,width=75)
        self.botonGraph.place(x=62,y=201)
        self.textMaxMin = Text(main_window,width=211,height=131)
        self.textMaxMin.place(x=245,y=147)
        (...)
root = Tk()
root.config(width=480,height=337)
mainW = ttk.Frame(root,width=480,height=337)
mainW.pack()
root.resizable(0,0)
app = MiApp(mainW)
app.mainloop()

现在,我的 Tkinter GUI 看起来像这样:

Tkinter 中的图形用户界面

如何将这些宽度值设置为像素?

感谢您的关注。

【问题讨论】:

  • 为什么需要以像素为单位设置宽度?在字符中使用宽度有什么问题?您可以使用字符宽度轻松获得所需的外观。使用grid 也可能比使用place 更容易让您不必做一堆数学运算。
  • 我需要设置宽度(以像素为单位),因为 GUI 数据取自 Qt Designer 的坐标和尺寸(以像素为单位),并且我需要设计尽可能清醒,所以我使用了Qt Designer 作为指南可以节省我一些时间,因为我认为 Tkinter 默认也使用像素(我错了哈哈哈)。

标签: python python-3.x tkinter


【解决方案1】:

我个人推荐我的项目https://github.com/cdhigh/tkinter-designer

tkinter-designer 在 VB6 中实现了一个插件,你可以在 VB6 中设计你的 GUI(拖放,调整大小,对齐,颜色,键绑定,...),然后这个插件生成一个完整的代码框架。您将要做的是在事件方法中添加逻辑代码,如在 VB 中编码。

PS:你可以安装nano版本的vb6。

【讨论】:

    【解决方案2】:

    我不知道,在创建 Frame 后在 place() 中定义宽度和高度允许按像素放置:

        def __init__(self,main_window):
        super().__init__(main_window)
        self.promDaily = []
        self.guidepromDaily = []
        self.datos = {}
        self.csv = " "
        self.minmax = " "
        self.botonCSV = ttk.Button(main_window,text="Escoger CSV",
             command=self.ChooseCSVFile)
        self.botonCSV.place(x=9,y=16,width=91)
        self.pathCSV = ttk.Entry(main_window,state="disable")
        self.pathCSV.place(x=120,y=17,width=351)
        self.labelAbsc = ttk.Label(main_window,text="Abscisa:")
        self.labelAbsc.place(x=9,y=52,width=47)
        self.labelOrden = ttk.Label(main_window,text="Ordenada:")
        self.labelOrden.place(x=120,y=52,width=55)
        self.labelPromD = ttk.Label(main_window,text="Promedio diario:")
        self.labelPromD.place(x=245,y=52,width=95)
        self.comboBAbsc = ttk.Combobox(main_window,state="readonly")
        self.comboBAbsc.place(x=9,y=78,width=91)
        self.comboBOrden = ttk.Combobox(main_window,state="readonly")
        self.comboBOrden.place(x=120, y=78, width=91)
        self.botonCalc = ttk.Button(main_window,text="Calcular", 
            command=self.PromedioDiario)
        self.botonCalc.place(x=245,y=78,width=75)
        self.posibilidadCalc = ttk.Entry(main_window,state="readonly")
        self.posibilidadCalc.place(x=245,y=114,width=211)
        self.botonGraph = ttk.Button(main_window,text="Graficar",
            command=self.GraphCSV)
        self.botonGraph.place(x=62,y=201,width=75)
        self.textMaxMin = Text(main_window,state="disabled")
        self.textMaxMin.place(x=245,y=147, width=211, height=131)
    

    Tkinter 中所需的 GUI 设计

    【讨论】:

    • 每个库都有默认的小部件几何尺寸,因此 tkinter 按钮的默认宽度可能与 Qt 不同。
    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 2018-07-04
    • 2018-02-27
    • 1970-01-01
    • 2011-08-25
    • 2015-07-01
    • 1970-01-01
    • 2012-05-14
    相关资源
    最近更新 更多