鱼C论坛

 找回密码
 立即注册
查看: 2511|回复: 4

[已解决]tkinter里的像素宽和高感觉不成比例

[复制链接]
发表于 2023-2-7 15:51:52 | 显示全部楼层 |阅读模式
5鱼币
tkinter里的宽和高的值的单位都是像素, 但怎么实际在图上完全不成比例.
导致每次调整界面时都没办法从数据下手, 都需要不断的去试.
请问有大师知道是怎么回事吗?
import tkinter as tk

root = tk.Tk()
root.title("It is not like that")
root.geometry('300x130')

e1 = tk.Button(root,text='First',height=2,width=7,fg='red')
e2 = tk.Button(root,text='Second',height=4,width=14,fg='blue')
e1.grid(row=0,column=0)
e2.grid(row=0,column=1)

root.mainloop()

最佳答案
2023-2-7 15:51:53
from tkinter import * #将import tkinter as tk 改成 from tkinter import *


root = Tk()
root.title("It is not like that")
root.geometry('300x130')
pic=PhotoImage(width=1,height=1) #设置像素
e1 = Button(root,text='First',image=pic,height=12,width=57,fg='red',compound='center') #以像素作为单位,compound='center'内容置中
e2 = Button(root,text='Second',image=pic,height=14,width=74,fg='blue',compound='center') #以像素作为单位,compound='center'内容置中
e1.grid(row=0,column=0)
e2.grid(row=0,column=1)

root.mainloop()



tkinter像素问题.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 15:51:53 | 显示全部楼层    本楼为最佳答案   
from tkinter import * #将import tkinter as tk 改成 from tkinter import *


root = Tk()
root.title("It is not like that")
root.geometry('300x130')
pic=PhotoImage(width=1,height=1) #设置像素
e1 = Button(root,text='First',image=pic,height=12,width=57,fg='red',compound='center') #以像素作为单位,compound='center'内容置中
e2 = Button(root,text='Second',image=pic,height=14,width=74,fg='blue',compound='center') #以像素作为单位,compound='center'内容置中
e1.grid(row=0,column=0)
e2.grid(row=0,column=1)

root.mainloop()



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 17:22:45 | 显示全部楼层
这是一个很有意义的问题,我也被困扰过
我的想法(不喜勿喷):
1 tk(懒得打全)的单位肯定是特殊单位,而且除了root.geometry和xxx.place()的大小单位是像素,其他可能不是
2 tk.xxx()的行列单位可能不一样,导致1x1的大小可能不接近正方形。(没证实过)比如你可以试试不一样的button,比如B1的w和b2的w可能成比例,h也是
3 我觉得tkinter的单位跟文字大小有关
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 19:20:40 | 显示全部楼层
tkinter除了窗口大小, 其他的单位都是字体的长宽, 如Button的width = 7表示有7个字的宽度
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 20:49:42 | 显示全部楼层
试着用place()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-24 17:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表