Python Tkinter未检测到用于GUI大小调整的正确屏幕分辨率
我有一个非常具体的问题,我一直在努力解决一段时间了。我正在Mac上的Pycharm中使用Python 2.7。我正在开发一个触摸屏GUI,以便与机器人进行交互。我遇到的问题是,我正在尝试使这个GUI在多个平台和不同的显示尺寸上看起来尽可能相似,就像按钮和标签大小一样 我尝试使用python来检测显示分辨率,并以此为基础将元素/小部件大小作为分辨率的比率。这在使用我的MacBookPro Retina 15时效果很好“分辨率为1440x900,以及分辨率设置为1280x800的覆盆子Pi,但当我尝试在分辨率为1920x1080的PC上执行此操作时,它不知何故检测到高度为1135,而不是1080。我创建了两个框架self.f1和self.f2,它们位于self.f1的中心,有趣的是,在我的Windows GUI中,self.f2的高度比self.f1的高度大,我认为这可能是我的问题的一部分。以下是我的程序GUI部分的特定代码:Python Tkinter未检测到用于GUI大小调整的正确屏幕分辨率,python,user-interface,tkinter,resize,screen-resolution,Python,User Interface,Tkinter,Resize,Screen Resolution,我有一个非常具体的问题,我一直在努力解决一段时间了。我正在Mac上的Pycharm中使用Python 2.7。我正在开发一个触摸屏GUI,以便与机器人进行交互。我遇到的问题是,我正在尝试使这个GUI在多个平台和不同的显示尺寸上看起来尽可能相似,就像按钮和标签大小一样 我尝试使用python来检测显示分辨率,并以此为基础将元素/小部件大小作为分辨率的比率。这在使用我的MacBookPro Retina 15时效果很好“分辨率为1440x900,以及分辨率设置为1280x800的覆盆子Pi,但当我尝
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(第二幅图像)上运行不同分辨率检索命令时得到的值
如果有人对我如何使这个GUI在任何屏幕大小上都像这样有任何建议或指导,我将不胜感激。窗口本身不会被调整大小一旦启动,但我只是想让它去全屏和按钮的大小很好。
在这里,尝试以下方法:
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如何自动调整大小 在这里,试试这个:
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如何自动调整大小我不知道你的问题的答案,但我更好奇为什么你自己这么做,而不是让Python处理所有的缩放?@小说,我仍然认为自己是Python新的,我不知道另一种方式来做到这一点。有没有一种方法可以控制我的按钮和标签的大小,你会建议我研究一下,看看是否适合我的项目?谢谢你,我不知道你的问题的答案,但我更好奇你为什么这样做,而不是让Python处理所有的缩放?@小说,我仍然认为自己是新的Python,我不知道另一种方式来做到这一点。有没有一种方法可以控制我的按钮和标签的大小,你会建议我研究一下,看看是否适合我的项目?谢谢你举个例子。这正是我所期待的,因为它在Mac、Linux和Windows上都能正常工作。我将使用您的示例并尝试用它更新我的程序,以使横幅和状态栏也得以扩展。谢谢你的回答!谢谢你的例子。这正是我所期待的,因为它在Mac、Linux和Windows上都能正常工作。我将使用您的示例并尝试用它更新我的程序,以使横幅和状态栏也得以扩展。谢谢你的回答!