如何在不更改按钮大小的情况下更改 tkinter 中 VKeyboard 的字体大小? [英] How to change the Font size of VKeyboard in tkinter without changing the size of button?

查看:30
本文介绍了如何在不更改按钮大小的情况下更改 tkinter 中 VKeyboard 的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索了解决方案,我得到了我们可以使用 Tkinter.Font 使用代码更改字体大小

I searched on web for the solution and I got we can change the font size using Tkinter.Font using the code

helv36 = tkFont.Font(family='Helvetica', size=32, weight=tkFont.BOLD)

以下是我试图增加按钮内文本字体大小的代码,但随着文本大小的增加,它也增加了按钮大小,这是我不想要的.

Following is the code that I tried to increase the font size of text inside the button but with increasing the size of the text, it's also increasing the button size and that's I don't want.

from tkinter import *
from tkinter import font as tkFont


Keyboard_App = Tk()

canvas = Canvas(Keyboard_App, width=400, height=400)
canvas.place(x=0, y=0, relwidth=1, relheight=1)


background = PhotoImage(file="Images/background.png")
canvas.create_image(400,400,image=background, tags="B")

buttons = [
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '=',
    'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '<-',
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '"',
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 'SHIFT',
    ' Space ',
]
curBut = [-1,-1]
buttonL = [[]]
canvas_textbox = canvas.create_text(0, 250, text='', anchor=NW,fill="cyan")


varRow = 1
varColumn = 0

def leftKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 4:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [0,10]
        buttonL[0][10].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [curBut[0], (curBut[1]-1)%11]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def rightKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 4:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [curBut[0], (curBut[1]+1)%11]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def upKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 0:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]-1)%5, 0]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]-1)%5, curBut[1]]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def downKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 3:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]+1)%5, 0]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]+1)%5, curBut[1]]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def select(value, x, y):
    if curBut != [-1,-1]:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
    curBut[:] = [x,y]
    buttonL[x][y].configure(highlightbackground='red')
    if value == "<-":
        input = entry.get("1.0", 'end-2c')
        entry.delete("1.0", END)
        entry.insert("1.0", input, END)

    elif value == " Space ":
        entry.insert(END, ' ')

    elif value == "Tab":
        entry.insert(END, '   ')

    else:
        entry.insert(END, value)

helv36 = tkFont.Font(family='Helvetica', size=12, weight=tkFont.BOLD)

for button in buttons:
    if button != " Space ":
        but = Button(Keyboard_App, text=button, font=helv36, width=10, height=3, bg="#000000", fg="#ffffff", highlightthickness=4, 
                       activebackground="#ffffff", activeforeground="#000000", relief="raised", padx=12,
                       pady=4, bd=4, command=lambda x=button, i=varRow-1, j=varColumn: select(x, i, j))
        buttonL[varRow-1].append(but)
        but.grid(row=varRow, column=varColumn)

    if button == " Space ":
        but = Button(Keyboard_App, text=button, font=helv36, width=60, bg="#000000", fg="#ffffff", highlightthickness=4, 
                       activebackground="#ffffff", activeforeground="#000000", relief="raised", padx=4,
                       pady=4, bd=4, command=lambda x=button, i=varRow-1, j=varColumn: select(x, i, j))
        buttonL[varRow-1].append(but)
        but.grid(row=6, columnspan=16)

    varColumn += 1
    if varColumn > 10:
        varColumn = 0
        varRow += 1
        buttonL.append([])

Keyboard_App.bind('<Left>', leftKey)
Keyboard_App.bind('<Right>', rightKey)
Keyboard_App.bind('<Up>', upKey)
Keyboard_App.bind('<Down>', downKey)
Keyboard_App.mainloop()

可以直接复制粘贴上面的代码,执行输出.我越是增加字体的大小,按钮的大小就会自动增加,这会导致 UI 的外观和感觉很糟糕.

You can directly copy and paste the above code and execute for the output. The more I increase the size of the font, thee size of button automatically increase and this results in a bad look and feel of UI.

我哪里出错了?

推荐答案

你可以尝试在but中减少padxpady.让我知道它是否有效

You can try decreasing padx and pady in but. Let me know if it works

这篇关于如何在不更改按钮大小的情况下更改 tkinter 中 VKeyboard 的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆