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

问题描述 投票:0回答:1

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

我尝试做的是使用python来检测显示分辨率,并从中将元素/小部件大小作为分辨率的比率。当使用分辨率为1440x900的MacBook Pro Retina 15“以及分辨率设置为1280x800的Raspberry Pi时,这种方法运行得很好但是当我尝试在分辨率为1920x1080的PC上进行此操作时,它会以某种方式检测高度我有两个框架创建self.f1和self.f2,它们位于self.f1的中心位置,有趣的是,在我的Windows上的GUI中,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(第一张图像)和Windows(第二张图像)上运行不同分辨率检索命令时获得的值。

From Windows PC From Windows PC

如果有人对如何在任何屏幕尺寸上使这个GUI看起来像这样有任何建议或指导,那将非常感激。一旦启动,窗口本身将不会调整大小,但我只是希望它全屏显示并且按钮尺寸很好。 enter image description here

python user-interface tkinter resize screen-resolution
1个回答
2
投票

在这里,试试这个:

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如何处理自动调整大小。

© www.soinside.com 2019 - 2024. All rights reserved.