Python Tkinter 未检测到用于 GUI 大小调整的正确屏幕分辨率

Python Tkinter Not Detecting Correct Screen Resolution to Use for GUI Sizing

我有一个非常具体的问题,一段时间以来我一直在努力解决这个问题。我在 Mac 上的 Pycharm 中使用 Python 2.7。我正在开发触摸屏 GUI 来与机器人交互。我遇到的问题是,我试图让这个 GUI 在多个平台和不同的显示尺寸上看起来尽可能相似,就按钮和标签大小而言。

我尝试做的是使用 python 来检测显示分辨率,然后将 elements/widgets 大小作为分辨率的比率。当我使用分辨率为 1440x900 的 MacBook Pro Retina 15" 以及分辨率设置为 1280x800 的 Raspberry Pi 时效果很好,但是当我尝试在具有分辨率的 PC 上执行此操作时1920x1080,它以某种方式检测到高度为 1135 而不是 1080。我创建了两个框架 self.f1 和 self.f2,它们位于 self.f1 的中心,有趣的是在我的 GUI 上 Windows ,不知何故self.f2的高度大于self.f1的高度我认为这可能是我的问题的一部分。这是我程序的GUI部分的具体代码:

def gui_layout(self):
    x_default = .5
    y_default = .52

    if self.version[1] == "1":
        x_off = x_default
        y_off = .535
    elif self.version[1] == "2":
        x_off = x_default
        y_off = y_default
    else:
        x_off = x_default
        y_off = y_default

    # GUI framework initialization
    self.title("Operator Interface - " + self.version)
    self.w_0, self.h_0 = self.winfo_screenwidth(), self.winfo_screenheight()
    self.geometry("%dx%d+0+0" % (self.w_0, self.h_0))
    # self.overrideredirect(True)
    self.wm_attributes('-fullscreen', 'True')

    self.f1 = tk.Frame(width=self.w_0, height=self.h_0, bg="white")
    self.f2 = tk.Frame(bg="black")

    self.f1.pack(fill="both", expand=True)
    self.f2.place(in_=self.f1, anchor="c", relx=x_off, rely=y_off)

    # Create status bar objects
    if self.version[1] == "2":
        self.state_text = " Status: %s  |" % self.status.mission_text
        self.state_bar = tk.Label(self.f1, text=self.state_text, bd=1, relief='sunken', anchor='w',
                                  font=("Helvetica", 20))
        self.state_bar.pack(side='bottom', fill='x')
    else:
        pass

    self.status_text = " State: %s  |  Current Mission: %s  |  Battery: %s" % (
        self.status.state_text, "Mission Name", self.status.battery_percentage) + "%" + "(%ss)  |  Network: %s" % (
        self.status.battery_time, "Network Status")
    self.status_bar = tk.Label(self.f1, text=self.status_text, bd=1, relief='sunken', anchor='w',
                               font=("Helvetica", 20))
    self.status_bar.pack(side='bottom', fill='x')
    self.orig_color = self.status_bar.cget("background")

    self.image_0 = Image.open("%s/Images/MIR.bmp" % self.folder_path)
    self.photo_0 = ImageTk.PhotoImage(self.image_0)

    self.image_0_label = tk.Label(self.f1, image=self.photo_0)
    self.image_0_label.image = self.photo_0

    self.w = self.w_0 / 70
    self.h = self.h_0 / 90
    self.w_1 = self.w_0 / 96
    self.h_1 = self.h_0 / 450

    self.button_1 = tk.Button(self.f2, text="Button 1", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w, height=self.h)
    self.button_2 = tk.Button(self.f2, text="Button 2", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w, height=self.h)
    self.button_3 = tk.Button(self.f2, text="Button 3", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w, height=self.h)
    self.button_4 = tk.Button(self.f2, text="Button 4", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w, height=self.h)
    self.button_5 = tk.Button(self.f2, text="Button 5", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w, height=self.h)
    self.button_6 = tk.Button(self.f2, text="Button 6", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w, height=self.h)
    self.button_7 = tk.Button(self.f2, text="Button 7", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w_1, height=self.h_1)
    self.button_8 = tk.Button(self.f2, text="Button 8", command=lambda: self.dummy_method(),
                              font=("Helvetica", 20), width=self.w_1, height=self.h_1)
    self.button_9 = tk.Button(self.f2, text="Button 9", command=lambda: self.help(), font=("Helvetica", 20),
                              width=self.w_1, height=self.h_1)

    self.image_0_label.pack(fill='x')
    self.button_1.grid(row=2, column=2, padx=20, pady=10)
    self.button_2.grid(row=2, column=3, padx=20, pady=10)
    self.button_3.grid(row=2, column=4, padx=20, pady=10)
    self.button_4.grid(row=3, column=2, padx=20, pady=10)
    self.button_5.grid(row=3, column=3, padx=20, pady=10)
    self.button_6.grid(row=3, column=4, padx=20, pady=10)
    self.button_7.grid(row=4, column=2, padx=20, pady=10)
    self.button_8.grid(row=4, column=3, padx=20, pady=10)
    self.button_9.grid(row=4, column=4, padx=20, pady=10)

    self.button_configure()

这也是我在 Pi(第 1 张图像)和 Windows(第 2 张图像)上执行 运行 不同分辨率检索命令时得到的值。

如果有人对我如何使此 GUI 在任何屏幕尺寸上看起来像这样有任何建议或指导,将不胜感激。 window 本身不会在启动后调整大小,但我只是希望它全屏显示并且按钮大小合适。

在这里,试试这个:

import tkinter as tk

root = tk.Tk()

# set columns 0, 1 and 2 and rows 0 and 1 to expand to the full possible size
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.columnconfigure(2, weight=1)
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)

# row zero
btn = tk.Button(root, text="Call")
btn.grid(row=0, column=0, sticky='nsew', padx=5, pady=5)
btn = tk.Button(root, text="Send to Sawyer")
btn.grid(row=0, column=1, sticky='nsew', padx=5, pady=5)
btn = tk.Button(root, text="Send to Compressor")
btn.grid(row=0, column=2, sticky='nsew', padx=5, pady=5)

# row one
btn = tk.Button(root, text="Send to Loading Dock")
btn.grid(row=1, column=0, sticky='nsew', padx=5, pady=5)
btn = tk.Button(root, text="Send to Charging Dock")
btn.grid(row=1, column=1, sticky='nsew', padx=5, pady=5)
btn = tk.Button(root, text="MiR is on the Way")
btn.grid(row=1, column=2, sticky='nsew', padx=5, pady=5)

# row two
btn = tk.Button(root, text="Supervisor")
btn.grid(row=2, column=0, sticky='nsew', padx=5, pady=5)
btn = tk.Button(root, text="Setup")
btn.grid(row=2, column=1, sticky='nsew', padx=5, pady=5)
btn = tk.Button(root, text="Help")
btn.grid(row=2, column=2, sticky='nsew', padx=5, pady=5)

# row three
state_bar = tk.Label(root, text = " State: %s  |  Current Mission: %s  |  Battery: %s")
state_bar.grid(row=3, column=0, columnspan=3)

try:
    root.state('zoomed') # windows
except:
    root.attributes('-zoomed', True) # linux
root.mainloop()

调整它的大小,看看 tkinter 如何自动处理所有的大小。