1

每次我必须在 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 中的图形用户界面

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

感谢您的关注。

4

2 回答 2

1

我不知道,在创建 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 设计

于 2019-10-21T00:38:31.613 回答
0

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

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

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

于 2020-06-15T21:41:15.890 回答