Cosimo Sguanci
Cosimo Sguanci

Reputation: 1291

Tkinter: how to enlarge the button text without increasing the size of the button

I have this code:

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()

(I'm on Raspberry Pi).

I get this result: image

There's the Button (left) and a label (right). I would like to mantain the size of the button but increase the text size to occupy the whole button. When i try, also the button becomes bigger. I think there's something like padding

Upvotes: 2

Views: 539

Answers (1)

Eddie
Eddie

Reputation: 1063

Im not sure if it is even possible with tkinter. I believe your best bet would be to slap an image.

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()

This is how it looks like

enter image description here

Also, every time you ask a question on Stack Overflow, please try your best to refactor the code, strip it down and post a minimum but a complete code.

Upvotes: 1

Related Questions