如何将我的众多按钮激活功能集成到一个处理所有数字的功能中?

时间:2015-08-02 04:39:24

标签: python

好的,所以我有我的代码来替换数字或将它们切换为“*”符号,但是它分散了十个函数,因为它们是按钮激活的,并且我很好奇是否有可能需要十个处理十个不同数字并将其放入一个按钮或其他东西的按钮。这是我的整个代码:

import Tkinter
import re
from math import sqrt, modf
from numpy import arange
from random import randint
from Tkconstants import END
import sys


def remove_one():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('1', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('1', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_two():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('2', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('2', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_three():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('3', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('3', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_four():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('4', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('4', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_five():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('5', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('5', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_six():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('6', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('6', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_seven():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('7', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('7', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_eight():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('8', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('8', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_nine():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('9', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('9', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def remove_zero():
    h = randint(1,2)  # create 1/2 and 1/2 effect, 50% chance
    if h == 1:
        text = E1.get().replace('0', "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace('0', "*")
        E1.delete(0, END)
        E1.insert(0, text)


def clear_entry():  # clear entry line with blank string
    E1.delete(0, END)
    E1.insert(0, '')


def fun_with_reverse():
    text = str(E1.get())
    if re.search('[a-zA-Z]', text):  # Make sure it's numeric only
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text="Input numbers only!").grid(ipadx=5, ipady=5)
        master.title("Wrong input")
        master.mainloop()
    else:
        i = len(text) - 1
        result = ""
        while i > -1:
            result = result + text[i]
            i -= 1
        text = E1.get().replace('0', "")
        E1.delete(0, END)
        E1.insert(0, result)


def prime_number():
    if re.search('[a-zA-Z]', str(E1.get())):  # Make sure it's numeric only
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text="Input numbers only!").grid(ipadx=5, ipady=5)
        master.title("Wrong input")
        master.mainloop()
        sys.exit(0)
    x = float(E1.get())
    if x < 2:  # number can't be prime if it's less than 2
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text="Don't go below 2, it isn't' going to work!").grid(ipadx=5,
                                                                                                            ipady=5)
        master.title("Prime Failure")
        master.mainloop()
    else:
        n = 2
        while n < x:
            if x % n == 0:  # can't be prime if it can be divided by anything besides one or itself
                master = Tkinter.Tk()
                w = Tkinter.Message(master, padx=5, pady=5, text="The number is not prime!").grid(ipadx=5, ipady=5)
                master.title("Prime Failure")
                master.mainloop()
                break
            n += 1
        else:
            master = Tkinter.Tk()
            w = Tkinter.Message(master, padx=5, pady=5, text="The number is prime!").grid(ipadx=5, ipady=5)
            master.title("Prime Success")
            master.mainloop()


def radical():
    # Define variables needed
    if re.search('[a-zA-Z]', str(E1.get())):  # Make sure it's numeric only
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text="Input numbers only!").grid(ipadx=5, ipady=5)
        master.title("Wrong input")
        master.mainloop()
        sys.exit(0)
    a = float(E1.get())
    b = (a + 1)
    c = sqrt(b)
    e = sqrt(a)
    f = (unichr(0x221A))
    fractional, integral = modf(c)
    for integral in arange(integral, 0, -1):  # loop to get fractional1 == 0
        fractional1, integral1 = modf(a / (integral ** 2))
        print(fractional1)
        if fractional1 != 0:
            integral -= 1
            continue
        else:
            break
    '''while integral == integral:
        fractional1, integral1 = modf(a / (integral**2))
        print(fractional1)
        if fractional1 != 0:
            integral -= 1
            continue
        else: break'''
    d = (a / (integral ** 2))
    if integral.is_integer():
        integral = int(integral)
    if d.is_integer():
        d = int(d)
    if e.is_integer():
        e = int(e)
    # set conditions for output
    if d == 1:
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text=(integral)).grid(ipadx=5, ipady=5)
        master.title("Square Root")
        master.mainloop()
    elif integral == 1:
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text=(f, d, "or", e)).grid(ipadx=5, ipady=5)
        master.title("Simplified Radical")
        master.mainloop()
    else:
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text=(integral, f, d, "or", e)).grid(ipadx=5, ipady=5)
        master.title("Simplified Radical")
        master.mainloop()


def factorial():
    if re.search('[a-zA-Z]', str(E1.get())):
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text="Input numbers only!").grid(ipadx=5, ipady=5)
        master.title("Wrong input")
        master.mainloop()
    else:
        x = float(E1.get())
    if x > 170:  # anything 170+ results in 'inf', so create barrier
        master = Tkinter.Tk()
        w = Tkinter.Message(master, padx=5, pady=5, text="You can't go above 170!")
        w.pack()
        master.mainloop()
        sys.exit(0)
    fact = 1
    for i in arange(1, x + 1):  # use arange to allow for float values, although 4! == 24 and 4.1! == 120 (5! ==120)
        fact *= i
    if fact.is_integer():
        fact = int(fact)
    master = Tkinter.Tk()
    w = Tkinter.Message(master, padx=5, pady=5, text=fact)
    w.config(font=('times', 19, 'italic'))
    w.pack()
    master.mainloop()
    return fact


def program_quit():
    Tkinter.sys.exit(0)  # Quit python
    sys.exit(0)


# define main GUI for program
root = Tkinter.Tk()
E1 = Tkinter.Entry(root)
assert isinstance(E1, object)
E1.grid(columnspan=2, ipadx=5, ipady=5, sticky='W''E')
B1 = Tkinter.Button(root, text='Determine Factorial', command=factorial).grid(row=1, ipadx=5, ipady=5, sticky='W''E')
B2 = Tkinter.Button(root, text='Exit', command=program_quit).grid(row=9, columnspan=2, ipadx=5, ipady=5, sticky='W''E')
B3 = Tkinter.Button(root, text='Simplify Radical', command=radical).grid(row=1, column=1, ipadx=5, ipady=5, sticky='W''E')
B4 = Tkinter.Button(root, text='Determine Prime', command=prime_number).grid(row=2, ipadx=5, ipady=5,
                                                                             sticky='W''E')
B5 = Tkinter.Button(root, text='Reverse the numbers', command=fun_with_reverse).grid(row=2, column=1, ipadx=5, ipady=5,
                                                                                     sticky='W''E')
B6 = Tkinter.Button(root, text="Remove/* 1's", command=remove_one).grid(row=3, ipadx=5, ipady=5, sticky='W''E')
B7 = Tkinter.Button(root, text="Remove/* 2's", command=remove_two).grid(row=3, column=1, ipadx=5, ipady=5, sticky='W''E')
B8 = Tkinter.Button(root, text="Remove/* 3's", command=remove_three).grid(row=4, ipadx=5, ipady=5, sticky='W''E')
B9 = Tkinter.Button(root, text="Remove/* 4's", command=remove_four).grid(row=4, column=1, ipadx=5, ipady=5, sticky='W''E')
B10 = Tkinter.Button(root, text="Remove/* 5's", command=remove_five).grid(row=5, ipadx=5, ipady=5, sticky='W''E')
B11 = Tkinter.Button(root, text="Remove/* 6's", command=remove_six).grid(row=5, column=1, ipadx=5, ipady=5, sticky='W''E')
B12 = Tkinter.Button(root, text="Remove/* 7's", command=remove_seven).grid(row=6, ipadx=5, ipady=5, sticky='W''E')
B13 = Tkinter.Button(root, text="Remove/* 8's", command=remove_eight).grid(row=6, column=1, ipadx=5, ipady=5, sticky='W''E')
B14 = Tkinter.Button(root, text="Remove/* 9's", command=remove_nine).grid(row=7,ipadx=5, ipady=5, sticky='W''E')
B15 = Tkinter.Button(root, text="Remove/* 0's", command=remove_zero).grid(row=7, column=1, ipadx=5, ipady=5, sticky='W''E')
B16 = Tkinter.Button(root, text="Clear Entry", command=clear_entry).grid(row=8, columnspan=2, ipadx=5, ipady=5, sticky='W''E')
root.mainloop()

所以,就像,如果我输入0123456789,我想要一个按钮,它将取出4或将其更改为*,然后我想改变2或7或其他东西,但我不想要一个窗口满是按钮或代码,其中包含使用不同数字执行相同操作的函数

1 个答案:

答案 0 :(得分:1)

像这样:

def remove_number(num):
    h = randint(1,2)
    if h == 1:
        text = E1.get().replace(num, "")
        E1.delete(0, END)
        E1.insert(0, text)
    else:
        text = E1.get().replace(num, "*")
        E1.delete(0, END)
        E1.insert(0, text)

这就是魔术:

buttons = []
for character in '0123456789':
    button = Tkinter.Button(root, text="button"+character,
                 command=lambda character=character: remove_num(character)))
    buttons.append(button)

实际上,我建议您使用EntryOptionMenu,用户可以选择该字符,然后选择一个Button,其命令可以获取所选的值。