微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tkinter 按钮在分辨率更高的电脑中错位

如何解决Tkinter 按钮在分辨率更高的电脑中错位

我在我的电脑上制作了一个 1366 x 768 分辨率的 tkinter 应用程序,然后转换为 exe 并发送到另一台分辨率为 1920 x 1080 的电脑。在那里,按钮放错了位置,进入了标签框 - 图像在中心,而我努力使用设置,使它们留在自己的位置,所以我尽可能避免像 place() 和使用的东西这样的东西都一样。顺便说一下,我的应用程序会根据电脑的分辨率调整自身大小,所以即使我在 4k 分辨率的电脑上运行它,它也能正常工作并在电脑屏幕上占据相同的位置。

"""
Pillow,pywin32,pyglet & pygame must be installed to run this code (click F5
to run)

To install those module you must write those codes one by one to CMD  
(command prompt) running as adminitrator (requires internet connection) :

- pip install pillow
- pip install pygame
- pip install pywin32
- pip install pyglet

If it gives ‘pip’ is not recognized as an internal or external command
error you must install it before running the codes aboves,to install it
follow this steps : https://datatofish.com/add-python-to-windows-path/

"""
#TO ADD A CUSTOM FONT(#ttf(font file)must be in the same place with the app
#which is 'RINGM___.TTF'):

import pyglet,tkinter
pyglet.font.add_file('RINGM___.TTF')

import PIL
from tkinter import *
from PIL import ImageTk,Image
from win32api import GetSystemMetrics
from tkinter import ttk

#TO disABLE PYGAME WELCOMING MESSAGE:
#import os
#os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"

import pygame
from pygame.locals import *

import time



# --- functions ---

def passit():
    pass

def backtonrml():
    RArrowbtn2.configure(image=useImg4)
    RArrowbtn2.update()

def backtonrml2():
    RArrowbtn.configure(image=useImg7)
    RArrowbtn.update()

def change_pic_2():
    RArrowbtn2.configure(image=useImg5)
    RArrowbtn2.update()

def change_pic_3():
    RArrowbtn.configure(image=useImg8)
    RArrowbtn.update()

def kill():
    pygame.mixer.music.stop()
    root.destroy()


def Oncursound():
        pygame.init()
        Locksoundobjempt = pygame.mixer.sound('slash sound rotwk.mp3')
        time.sleep(0.05)
        Locksoundobjempt.play()
        
        

        

def OnCursor():
    change_pic_2()
    Oncursound()        
        

def OnCursor2():
    change_pic_3()
    Oncursound()
    
    
    


root = Tk()


screen_x = int(GetSystemMetrics(0))
screen_y = int(GetSystemMetrics(1))

window_x = GetSystemMetrics(0) - 325
window_y = GetSystemMetrics(1) - 155

posX = (screen_x // 2) - (window_x // 2)
posY = (screen_y // 2) - (window_y // 2)

geo = "{}x{}+{}+{}". format(window_x,window_y,posX,posY)

root.geometry(geo)
root.config(bg="gray15",cursor='arrow red red')
root.update()

#root.overrideredirect(1)
print(root.winfo_width(),"x",root.winfo_height())
print(GetSystemMetrics(0),GetSystemMetrics(1))





imgW2 = root.winfo_width() - 250
imgH2 = root.winfo_height() - 150


imgW3 = root.winfo_width() - 225
imgH3 = root.winfo_height() - 125



img5=Image.open('3nCc3Sq.png')
image5=img5.resize((imgW2,imgH2))
bg=ImageTk.PhotoImage(image5)
  
# Create label

labelframe = LabelFrame(root,text="Click Left or Right Arrow to switch the theme",height=imgH3,width=imgW3,labelanchor='n',font = ("Ringbearer",19),fg='#5E66D6',bg="gray15")
labelframe.place(relx=.5,rely=.5,anchor="center")

label = Label(labelframe,image=bg,highlightbackground="red3",bd=0,relief='flat',)
label.pack()
label.image = bg

imgW = 97
imgH = 274

img4=Image.open('1.png')
image4=img4.resize((imgW,imgH))
useImg4=ImageTk.PhotoImage(image4)

img5=Image.open('2.png')
image5=img5.resize((imgW,imgH))
useImg5=ImageTk.PhotoImage(image5)

img6=Image.open('3.png')
image6=img6.resize((103,280))
useImg6=ImageTk.PhotoImage(image6)

#--------------------------------------------

Img7 = img4.transpose(PIL.Image.FLIP_LEFT_RIGHT)
Img7.save('4.png')
root.update()
Img7_2 = Image.open('4.png')
image7 = Img7_2.resize((imgW,imgH))
useImg7 = ImageTk.PhotoImage(image7)

Img8 = img5.transpose(PIL.Image.FLIP_LEFT_RIGHT)
Img8.save('5.png')
root.update()
Img8_2 = Image.open('5.png')
image8 = Img8_2.resize((imgW,imgH))
useImg8 = ImageTk.PhotoImage(image8)

Img9 = img6.transpose(PIL.Image.FLIP_LEFT_RIGHT)
Img9.save('6.png')
root.update()
Img9_2 = Image.open('6.png')
image9 = Img9_2.resize((imgW,imgH))
useImg9 = ImageTk.PhotoImage(image9)

  
def change_pic():
    pygame.init()
    btnsoundobj = pygame.mixer.sound('rotwk button sound.mp3')
    btnsoundobj.play()
    RArrowbtn2.configure(image=useImg6)
    RArrowbtn2.update()
    time.sleep(0.05)
    RArrowbtn2.configure(image=useImg5)
    RArrowbtn2.update()

def change_pic2():
    pygame.init()
    btnsoundobj = pygame.mixer.sound('rotwk button sound.mp3')
    btnsoundobj.play()
    RArrowbtn.configure(image=useImg9)
    RArrowbtn.update()
    time.sleep(0.05)
    RArrowbtn.configure(image=useImg8)
    RArrowbtn.update()

    

RArrowbtn = Button(root,image=useImg7,command=change_pic2,activebackground="gray15")
RArrowbtn.config(borderwidth=0,highlightthickness = 0,bd = 0,bg='gray15') 

RArrowbtn.place(relx=.06,anchor="center")



RArrowbtn2 = Button(root,image=useImg4,command=change_pic,activebackground="gray15")
RArrowbtn2.config(borderwidth=0,bg='gray15') 

RArrowbtn2.place(relx=.94,anchor="center")

pygame.init()
pygame.mixer.music.load('Lotropening-2.mp3')
pygame.mixer.music.play(0)



RArrowbtn.bind("<Enter>",lambda eff: OnCursor2())
RArrowbtn.bind("<Leave>",lambda eff: backtonrml2())



RArrowbtn2.bind("<Enter>",lambda eff: OnCursor())
RArrowbtn2.bind("<Leave>",lambda eff: backtonrml())



root.mainloop()

在我制作的电脑上看起来是这样的:

in the pc i made:

在另一台电脑上也是这样:

enter image description here

(我删除菜单

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。