显示基于计数器的Pi十进制数

时间:2018-04-24 04:15:41

标签: python-3.x math tkinter buttonclick pi

目前是python的初学者。

我正在尝试创建一个基于pi的简单tkinter应用程序(3.14)  每当他们按下“下一个数字”时,它会增加计数器+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上,例如。计数器0 = 3.14,计数器1 = 3.141,计数器2 = 3.1415等任何形式的帮助表示赞赏:)

1 个答案:

答案 0 :(得分:0)

你可以这样做:

你的应用程序大部分都没有调用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而不是混乱全局命名空间 函数try/except中的decimals()块在枚举完成时避免了令人讨厌的索引错误。
标签显示显示的小数,另一个显示其值 一旦完成,枚举就会重置为第一个小数。

编辑:

您可以执行以下操作,以便添加数字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()