【问题标题】:How to make Tkinter app work on different computers with different resolutions如何使 Tkinter 应用程序在不同分辨率的不同计算机上工作
【发布时间】:2023-02-23 00:40:54
【问题描述】:

我编写了一个 tkinter 应用程序,一切正常,没有出错。我使用 pyinstaller 将 tkinter .py 转换为 .exe。此应用程序应该能够通过远程计算机(台式机或笔记本电脑)进行访问。但问题是在尝试远程桌面时,应用程序无法打开。

我认为这是因为我使用 .place() 放置小部件和 xy 每台计算机的值都不相同。但我也有一些可以通过按下按钮清除的地块。为此,.get_tk_widget().place_forget() 更容易并且效果很好。而且我知道我不能在同一个程序中混合 gridplace

转换为 .exe 后如何使程序在任何计算机上运行?以下是更长代码的简化示例。

import tkinter as tk
from tkinter import ttk
from tkmacosx import Button
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
import numpy as np

plt.close("all")

class Window(tk.Tk):
    def __init__(self,*args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent,bg='#192232') 
        toolbar0 = tk.Frame(self,bg="#393951")
        toolbar0.pack(side=tk.TOP, fill=tk.X)
        label = ttk.Label(toolbar0, text="OLE Analyser")
        label.pack(pady=7,padx=10)
        button = Button(toolbar0, text="Analysis",command=lambda: controller.show_frame(PageOne))
        button.place(x=1600,y=6)   

class PageOne(tk.Frame):
    def __init__(self,parent, controller):
        tk.Frame.__init__(self, parent,bg='#192232')
        toolbar1 = tk.Frame(self,bg="#393951")
        toolbar1.pack(side=tk.TOP, fill=tk.X)
        
        button1 = Button(toolbar1,text="Home",command=lambda: controller.show_frame(StartPage))
        button1.place(x=1600,y=6)
        button2 = Button(toolbar1,text="Comparison",command=lambda: controller.show_frame(PageTwo))
        button2.place(x=1700,y=6) 

        fig, ax = plt.subplots(figsize=(9.05,6.43))
        canvas1 = FigureCanvasTkAgg(fig,self) 
        
        x1 = np.arange(1,100,2)
        y1 = x1**2 
        ax.plot(x1,y1)
        canvas1.draw()
        canvas1.get_tk_widget().place(x=272,y=103)
        
        def clear_graph():
            canvas1.get_tk_widget().place_forget() 
            
        clear_btn = Button(self,text="Clear Graph",command=lambda:clear_graph())
        clear_btn.place(x=165,y=927)        
   
class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent,bg='#192232')
        toolbar2 = tk.Frame(self,bg="#393951")
        toolbar2.pack(side=tk.TOP, fill=tk.X)
 
if __name__ == "__main__":
    app = Window()             
    ww = 1920
    hw = 1009
                
    w = app.winfo_screenwidth()
    h = app.winfo_screenheight() 
        
    x = (w/2) - (ww/2)-8
    y = (h/2) - (hw/2)-36
            
    app.geometry('%dx%d+%d+%d' % (ww, hw, x, y))
    app.update()
    app.mainloop()```

【问题讨论】:

  • “而且我知道我不能在同一个程序中混合使用网格和位置。” ~ 是的,你可以。您不能将gridpack 混合使用小部件. IE。如果您在 frame 中使用 pack,则不能在同一个 frame 中也使用 grid。无论如何,您可以随意使用place
  • 如果我使用grid,我还能为不同的xs 和ys 使用自定义位置吗?它会解决代码在不同计算机上无法运行的问题吗?
  • grid 用于将小部件与行/列方法对齐。 pack 用于将小部件“停靠”到可用边缘,即使该边缘是由另一个小部件创建的。 place 用于将小部件放置在特定的 x/y 坐标处。我不明白你的问题,我不知道它是否能解决你的问题。

标签: python tkinter ttkwidgets


【解决方案1】:

我对这个问题的最佳回答是你将 app.geometry 设置为一个特定的数字,例如;

app.geometry("800x600")

使用它,您可以更改 x 和 y 值以将它们放入这样的框架中,从而允许跨分辨率的多余使用。此外,根据您的用例,使用

grid()

可能比您当前的按钮实现方法更合适,因为它为您的 x 和 y 值创造了更少的空间,以便在不一定依赖 x 和 y 精确放置的程序中严重偏离。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2019-10-17
    • 1970-01-01
    相关资源
    最近更新 更多