1

Generator functions promise to make some code easier to write. But I don't always understand how to use them.

Let's say I have a Fibonacci generator function fib(), and I want a tkinter application that displays the first result. When I click on a button "Next" it displays the second number, and so on. How can I structure the app to do that?

I probably need to run the generator in a thread. But how do I connect it back to the GUI?

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
  • Well it's a little bit like everything else you do with a generator and a threaded application. I'm not sure what you actually need help with. – Thingamabobs Feb 08 '23 at 12:10
  • If you want to, click on my profile and join the tkinter chat will be there after getting a coffee. – Thingamabobs Feb 08 '23 at 12:11
  • [threaded tkinter app examples](https://stackoverflow.com/a/75102903/13629335) and in here is a [generator used with tkinter](https://stackoverflow.com/a/71288931/13629335). – Thingamabobs Feb 08 '23 at 12:28
  • You don't need threading for this task. – Bryan Oakley Feb 08 '23 at 14:49
  • @Thingamabobs I wasn't aware that I could use the generator object by itself. I was assuming I have to structure the code around a for loop. – Andreas Haferburg Feb 08 '23 at 17:36
  • @AndreasHaferburg as python is an interpreted language, the syntax yield makes it an generator. But there are good articles about it and some good answers on [so]. It's a fundamental difference to C that you seem to be used to. – Thingamabobs Feb 08 '23 at 17:57

2 Answers2

3

You don't need a thread for this particular problem. You just need to instantiate the generator, then use next to get a new value each time the button is pressed.

Here's a simple example:

import tkinter as tk

def fibonacci_sequence():
    '''Generator of numbers in a fibonacci sequence'''
    a,b = 1,1
    while True:
        yield a
        a,b = b, a+b

def do_next():
    '''Updates the label with the next value from the generator'''
    label.configure(text=next(generator))

root = tk.Tk()
label = tk.Label(root, text="")
button = tk.Button(root, text="Next", command=do_next)
label.pack(side="top")
button.pack(side="bottom")

# Initialize the generator, then us it to initialize
# the label
generator = fibonacci_sequence()
do_next()

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • While the numbers and the processing takes longer over tume, it might be a good idea to use a thread or process. I still don't know what OP is actually asking for. – Thingamabobs Feb 08 '23 at 15:26
2

Not expert in tkinter but one way is to use function StringVar to be able to update its value and using text box for output. Try this out, hope it gives you a clue.

import tkinter as tk

fibo_list = [1,1]
def fibo():
    fibo_list.append(fibo_list[-1] + fibo_list[-2])
    variable.set(fibo_list[-1])
    show_fibo_value()

def show_fibo_value():
    text_box.insert(index='end',chars=variable.get()+'\n')
root = tk.Tk()
variable = tk.StringVar()
text_box = tk.Text()
text_box.pack()
button = tk.Button(root, text="Next", command=fibo)
button.pack()

root.mainloop()
pouya
  • 81
  • 5