Currently a beginner in python.
I'm trying to create a simple tkinter application based on pi (3.14) Every time they press "Next Digit" it increases the counter +1 and displays the next decimal number.
# Import the Tkinter functions
from tkinter import *
#####backend function
global counter
counter = 0
pi = '159265358979323846'
def pi_decimal():
while counter > 0
pass
def Decimals():
global counter
counter +=1
display_decimal['text'] = counter
# Create a window
application_window = Tk()
# Give the window a title
application_window.title('PI')
# create a button for application window
my_button = Button(application_window,
text = 'Next Digit', command = Decimals)
#create a row that shows the number
number_pi = Label(application_window, text = pi)
display_decimal = Label(application_window, text = counter)
#put button into the window
display_decimal.pack()
number_pi.pack()
my_button.pack()
I'm trying to figure out how to go through the list of decimals of upto 18 adding onto 3.14, for instance. counter 0 = 3.14, counter 1 = 3.141, counter 2 = 3.1415 etc. Any kind of help is appreciated :)
You could do something like this:
Your app was mostly missing a call to mainloop.
# Import the Tkinter functions
import tkinter as tk
def decimals():
global counter
counter += 1
try:
text = pi[counter]
except IndexError:
counter = 0
text = ''
display_decimal['text'] = f"PI decimal value: {text}"
display_decimal_ndx['text'] = f"PI decimal index: {counter}"
if __name__ == '__main__':
# backend function
counter = 0
pi = '159265358979323846'
# Create a window
application_window = tk.Tk()
# Give the window a title
application_window.title('PI')
# create a button for application window
my_button = tk.Button(application_window,
text='Next Digit', command=decimals)
# create a row that shows the number
number_pi = tk.Label(application_window, text = pi)
display_decimal = tk.Label(application_window, text='')
display_decimal_ndx = tk.Label(application_window, text=counter)
# put button into the window
display_decimal.pack()
display_decimal_ndx.pack()
number_pi.pack()
my_button.pack()
application_window.mainloop()
You may want to consider using import tkinter as tk
instead of cluttering the global namespace.
A try/except
block in the function decimals()
avoids pesky index errors when the enumeration is done.
A label shows which decimal is shown, and another shows its value.
The enumeration resets to the first decimal once it is done.
You could do something like this in order to add a display of number Pi that updates the number of decimals displayed as we press the button:
import tkinter as tk
def decimals():
global counter
counter += 1
try:
text = pi[counter]
except IndexError:
counter = 4
text = ''
display_decimal['text'] = f"PI decimal value: {text}"
display_decimal_ndx['text'] = f"PI decimal index: {counter}"
number_pi['text'] = pi[:counter+1]
if __name__ == '__main__':
# backend function
counter = 4
pi = '3.14159265358979323846'
# Create a window
application_window = tk.Tk()
# Give the window a title
application_window.title('PI')
# create a button for application window
my_button = tk.Button(application_window,
text='Next Digit', command=decimals)
# create a row that shows the number
number_pi = tk.Label(application_window, text = pi[:counter+1])
display_decimal = tk.Label(application_window, text='')
display_decimal_ndx = tk.Label(application_window, text=counter)
# put button into the window
display_decimal.pack()
display_decimal_ndx.pack()
number_pi.pack()
my_button.pack()
application_window.mainloop()