Daveolw1
Posts: 23
Joined: Thu May 16, 2019 10:24 am

Using button to display text in tKinter text wiget

Wed Jun 05, 2019 5:02 pm

Trying to use a button to add text to the graphic screen using a tKinter text wiget.

The line " T.insert("1.0", line1)" fails to work, if I remove the "#" from the two lines:-
#screen_display_var = 1
#run_program = False
it works just running through that piece of code, but not from the button
It does go through the code but does not display the text on the window, the "print" output on the Python shell shows it went through.

If screen_display_var = 1
ABBA FATHER, let me be
end screen_display_var = 0

Code: Select all

import RPi.GPIO as GPIO  
import time
GPIO.setmode(GPIO.BCM)  
from tkinter import *
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.virtual import viewport, sevensegment
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 root = Tk()
T = Text(root, bg='blue', height=10, width=15)
T.config(foreground='yellow', padx=80, pady=80, font=('Times New Roman', 48, "bold"))
T.pack()
# create seven segment device
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, cascaded=1)
seg = sevensegment(device)
seg.text = ("0")
run_program = True
global screen_display_var
screen_display_var = 0
global digit1_value
digit1_value = 0
def my_callbackp4(channel):  
    global digit1_value
    digit1_value += 1
    if digit1_value  >= 10:
        digit1_value = 0  
        seg.text = str(digit1_value) 
 def my_callbackp20(channel):
      global screen_display_var
      screen_display_var = 1
GPIO.add_event_detect(4, GPIO.FALLING, callback=my_callbackp4, bouncetime=300)
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callbackp20, bouncetime=400)
while run_program:
    try:
        #screen_display_var = 1
        if screen_display_var == 1:
             print('If screen_display_var = ', screen_display_var)
             f = open("/home/pi/Documents/song2.txt", "r")
             line1 = f.readline()
             print(line1)
             T.insert("1.0", line1)
             screen_display_var = 0
             print('end screen_display_var = ', screen_display_var)
   except KeyboardInterrupt:
        print("Exception thrown -> Abort")
        GPIO.cleanup()
   #run_program = False
root.mainloop()
    


Any ideas how to solve this.

Thanks David
Last edited by Daveolw1 on Thu Jun 06, 2019 6:47 am, edited 1 time in total.

User avatar
Paeryn
Posts: 2636
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Using button to display text in tKinter text wiget

Wed Jun 05, 2019 11:15 pm

You don't seem to be running the Tk mainloop anywhere so no updates or events will be processed. You insert the text at the start of the Text object but without the mainloop running the Text object will never be redrawn.

Usually Tk based programs are written in an event based manner where your program (mainly) responds to events. You tell Tk what procedures to run on certain events and then call Tk.mainloop(). Tk.mainloop() will then sit in an infinite loop (or until an event signals it to quit) updating its windows and calling your procedures when events happen.

You can have your program running like you do but you need to keep telling Tk to run through its update and event handling. So your loop would have to begin something like

Code: Select all

while run_program:
    root.update_idletasks()
    root.update()
    # your code here.
Though if the rest of your while loop takes much time then your windows will not be very responsive. Generally this isn't the way to write code for GUIs.
She who travels light — forgot something.

Daveolw1
Posts: 23
Joined: Thu May 16, 2019 10:24 am

Re: Using button to display text in tKinter text wiget

Thu Jun 06, 2019 6:27 pm

Thanks for the reply, actually I did have the "root.mainloop" there it was the last line of the code but for some reason it got lost in the copy and paste. I have great difficulty finding tutorials for tkinter and python, a lot of example code not working. To give one simple example a lot of the examples given give "print "xxxxx" when I use that it states brackets missing.
I had got the Gui working ok as a separate project, and the GPIO as buttons working as another project and now I need to put the two together.
The project is to display on HDMI monitor the words of songs which each song is in a separate file, and will be selected by the information on the sevensegment display. I have the file selection working and selection of which lines to display and just need to work out how pressing a button will display the selected lines from the selected file, clear display and show more lines.
You say "Generally this isn't the way to write code for GUIs." So where do I get the information to write it the correct way?

Thanks David

User avatar
Paeryn
Posts: 2636
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Using button to display text in tKinter text wiget

Fri Jun 07, 2019 1:10 am

Daveolw1 wrote:
Thu Jun 06, 2019 6:27 pm
Thanks for the reply, actually I did have the "root.mainloop" there it was the last line of the code but for some reason it got lost in the copy and paste. I have great difficulty finding tutorials for tkinter and python, a lot of example code not working. To give one simple example a lot of the examples given give "print "xxxxx" when I use that it states brackets missing.
I had got the Gui working ok as a separate project, and the GPIO as buttons working as another project and now I need to put the two together.
The project is to display on HDMI monitor the words of songs which each song is in a separate file, and will be selected by the information on the sevensegment display. I have the file selection working and selection of which lines to display and just need to work out how pressing a button will display the selected lines from the selected file, clear display and show more lines.
You say "Generally this isn't the way to write code for GUIs." So where do I get the information to write it the correct way?

Thanks David
I see you've added the mainloop() now, but it wouldn't work as you hope as the Text object won't be updated until it gets to root.mainloop(), and from then on you don't have any code that updates the Text object (the only part that did was the while loop).

As to old example code having print statements without brackets, those are written for Python2 whereas you are using Python3, the other main difference you would see is the import name of tkinter, Python3 calls it tkinter whereas Python2 calls it Tkinter, but example code will still teach you how to use tkinter.

A quick search brought this up and it uses Python3, Introduction to GUI Programming with tkinter
She who travels light — forgot something.

Daveolw1
Posts: 23
Joined: Thu May 16, 2019 10:24 am

Re: Using button to display text in tKinter text wiget

Fri Jun 07, 2019 6:49 am

Thanks for the reply, that article will be very helpful.
David

Return to “Python”