基于关闭计数器显示 Pi 十进制数
Displaying Pi decimal number based off counter
目前是 python 的初学者。
我正在尝试创建一个基于 pi (3.14) 的简单 tkinter 应用程序
每次他们按 "Next Digit" 它都会增加计数器 +1 并显示
下一个十进制数。
# 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()
例如,我正在尝试弄清楚如何遍历最多 18 位的小数列表加上 3.14。 counter 0 = 3.14, counter 1 = 3.141, counter 2 = 3.1415 等等。感谢任何形式的帮助:)
你可以这样做:
您的应用主要缺少对 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()
注:
您可能要考虑使用 import tkinter as tk
而不是使全局命名空间混乱。
函数 decimals()
中的 try/except
块避免了枚举完成时讨厌的索引错误。
一个标签显示显示的是哪个小数,另一个标签显示其值。
一旦完成,枚举将重置为第一位小数。
编辑:
您可以这样做来添加数字 Pi 的显示,它会在我们按下按钮时更新显示的小数位数:
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()
目前是 python 的初学者。
我正在尝试创建一个基于 pi (3.14) 的简单 tkinter 应用程序 每次他们按 "Next Digit" 它都会增加计数器 +1 并显示 下一个十进制数。
# 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()
例如,我正在尝试弄清楚如何遍历最多 18 位的小数列表加上 3.14。 counter 0 = 3.14, counter 1 = 3.141, counter 2 = 3.1415 等等。感谢任何形式的帮助:)
你可以这样做:
您的应用主要缺少对 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()
注:
您可能要考虑使用 import tkinter as tk
而不是使全局命名空间混乱。
函数 decimals()
中的 try/except
块避免了枚举完成时讨厌的索引错误。
一个标签显示显示的是哪个小数,另一个标签显示其值。
一旦完成,枚举将重置为第一位小数。
编辑:
您可以这样做来添加数字 Pi 的显示,它会在我们按下按钮时更新显示的小数位数:
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()