Tkinter:如何在不增加按钮大小的情况下放大按钮文本

时间:2016-12-25 14:59:11

标签: python tkinter

我有这段代码:

import RPi.GPIO as GPIO
import time
from tkinter import *
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,pull_up_down=GPIO.PUD_UP) #Scrivi lettera/spazio
GPIO.setup(12,GPIO.IN, pull_up_down=GPIO.PUD_UP)#STICK: lettera -
GPIO.setup(5,GPIO.IN, pull_up_down=GPIO.PUD_UP) #STICK: lettera +
GPIO.setup(26,GPIO.IN, pull_up_down=GPIO.PUD_UP)#Cancella lettera
GPIO.setup(7,GPIO.IN, pull_up_down=GPIO.PUD_UP) #TTS
GPIO.setup(23,GPIO.IN, pull_up_down=GPIO.PUD_UP)#Spegnimento



x=0 #Variabile globale utile a determinare quale lettra mostrare
s="" #Stringa che serve a concatenare le lettere per  mostrare la parola


class mainGUI:
def __init__(self, parent):
    self.mioContenitore=Frame(parent)
    self.mioContenitore.pack()

    self.schermataSx=Button(self.mioContenitore)
    self.schermataSx["background"]="white"
    self.schermataSx.config(font=("helvetica",80))#vedere come ingrandire il testo senza utilizzare il button
    self.schermataSx.config(height=50, width=5)
    self.schermataSx.pack(side=LEFT)




    self.schermataDx=Label(self.mioContenitore,wraplength=450)
    self.schermataDx["background"]="white"
    self.schermataDx.config(font=("helvetica", 40)) #wraplength da rivedere
    self.schermataDx.config(height=50,width=30,anchor=W)
    self.schermataDx.pack(side=LEFT)



def cambioLett(self):
    global x
    global a
    if(GPIO.input(5)==0):
        time.sleep(1.0) # Ritardo cambio lettera da rivedere
        x+=1
    if(GPIO.input(12)==0):
        x-=1
    if(GPIO.input(7))==0: #TTS: Dice solo la prima parola
        os.system("pico2wave -l it-IT -w speak.wav "+s+" ")
        os.system("aplay speak.wav")

    if(GPIO.input(23))==0:
        os.system("sudo shutdown -h now")

    if x==0:
        self.schermataSx["text"]="A"
        time.sleep(0.25)
        if(GPIO.input(17)==0):
            time.sleep(0.25)
            cont=0
            while(GPIO.input(17))==0:
                cont=cont+1
                time.sleep(0.5)
            if cont>=5:
                self.scriviSpazio()
            else:
                self.scriviLett()

        if(GPIO.input(26))==0:
            self.cancellaLett()

...

def scriviLett(self):
    global s
    global x
    if(x==0):
        s+="A"
        self.schermataDx["text"]=s
    if(x==1):
        s+="B"
        self.schermataDx["text"]=s

...

def scriviSpazio(self):
    global x
    global s
    s+=" "


def cancellaLett(self):
    global s
    s=s[:-1]
    self.schermataDx["text"]=s


root=Tk()
root.attributes('-fullscreen',True)
GUI=mainGUI(root)
GUI.cambioLett()
root.mainloop()

(我在Raspberry Pi上)。

我得到了这个结果:  image

有按钮(左)和标签(右)。我想保留按钮的大小,但增加文本大小占据整个按钮。当我尝试时,按钮也变大了。我认为有像填充

之类的东西

1 个答案:

答案 0 :(得分:1)

我不确定tkinter是否可行。我相信你最好的选择是拍一张照片。

from tkinter import *

Tk()

background_image = PhotoImage(file='yin_yang.png')
button = Button(
    image=background_image,
    height=480,
    background='green'
)
button.pack(side=LEFT)

label = Label(
    font=('helvetica', 60),
    text='Hello, RPi :)',
)
label.pack(side=LEFT)

mainloop()

这就是它的样子

enter image description here

此外,每当您在Stack Overflow上提出问题时,请尽量重构代码,将其删除并发布最少但完整的代码。