如何将我的python代码集成到我的tkinter接口?

时间:2019-03-17 19:18:40

标签: python python-3.x tkinter

我制作了一个程序,用户在其中输入目标号码和其他4个数字作为目标号码。现在,我很难将我的tkinter接口集成到我的代码中。所以我希望有人能帮助我

Tkinter界面:

window = Tk()
window.title("target number solution")
Label(window,image='', bg="white") .grid(row=0, column=0, sticky=W)
Label(window, text="Enter target number:",bg="black", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=N)


Label(window, text="Enter first number:",bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=5, column=0, sticky=N)


Label(window, text="Enter second number:",bg="black", fg="white", font="none 12 bold").grid(row=6, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=7, column=0, sticky=N)


Label(window, text="Enter third number:",bg="black", fg="white", font="none 12 bold").grid(row=8, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=9, column=0, sticky=N)


Label(window, text="Enter fourth number:",bg="black", fg="white", font="none 12 bold").grid(row=10, column=0, sticky=N)
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=11, column=0, sticky=N)


Button(window, text="Solve", width=6, command=solver).grid(row=12, column=0, sticky=N)
output = Text(window, width=60, height=10, wrap=WORD, background="white")
output.grid(row=13, column=0, columnspan=1, sticky=N)


window.mainloop()

This is what the interface looks like

这是需要与tkinter接口链接的代码:

from itertools import permutations,combinations_with_replacement
numbers   = []
target    = int(input())
operators = ["+","-","*","/"]
groups    = ['X+X+X+X', 'X+X+(X+X)', 'X+(X+X)+X', '(X+X+X)+X', '(X+X)+X+X', 'X+(X+X+X)', '((X+X)+X)+X', 'X+(X+(X+X))', 'X+((X+X)+X)', '(X+X)+(X+X)', '(X+(X+X))+X']
seen      = set()
for values in permutations(numbers,len(numbers)):
    for operCombo in combinations_with_replacement(operators,len(numbers)-1):
        for oper in permutations(operCombo,len(numbers)-1):
            formulaKey = "".join(oper+values)
            if formulaKey in seen: continue # ignore variations on parentheses alone
            for pattern in groups:
                formula = "".join(o+p for o,p in zip([""]+list(oper), pattern.split("+")))
                formula = "".join(v+p for v,p in zip([""]+list(values),formula.split("X")))
                try:
                    if eval(formula) == target:
                        global Answer
                        Answer = formula,"=",target
                        print(formula,"=",target)
                        seen.add(formulaKey)
                        break
                except: pass

所有建议都会被应用

2 个答案:

答案 0 :(得分:1)

您可以将每个数字的值存储在单独的IntVar变量中,然后在solver函数中获取这些变量的值,并执行第二个代码中的所有操作。应该使用包含用于创建小部件并运行solver代码的函数的类对代码进行模块化。要了解有关Tkinter中基于类的声明的更多信息,请阅读this

from tkinter import Tk, Frame, Label, Button, IntVar, Entry, Text, W, N, WORD, INSERT
from itertools import permutations,combinations_with_replacement



class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets() 


    def create_widgets(self):

        self.target_num = IntVar()
        self.num1 = IntVar()
        self.num2 = IntVar()
        self.num3 = IntVar()
        self.num4 = IntVar()       
        # self.title("target number solution")

        Label(self,image='', bg="white").grid(row=0, column=0, sticky=W)
        Label(self, text="Enter target number:", bg="black", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=N)
        self.textentry1 = Entry(self, textvariable=self.target_num, width=20, bg="white")
        self.textentry1.grid(row=2, column=0, sticky=N)


        Label(self, text="Enter first number:",bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=N)
        self.textentry2 = Entry(self, textvariable=self.num1, width=20, bg="white")
        self.textentry2.grid(row=5, column=0, sticky=N)


        Label(self, text="Enter second number:",bg="black", fg="white", font="none 12 bold").grid(row=6, column=0, sticky=N)
        self.textentry3 = Entry(self, textvariable=self.num2, width=20, bg="white")
        self.textentry3.grid(row=7, column=0, sticky=N)


        Label(self, text="Enter third number:",bg="black", fg="white", font="none 12 bold").grid(row=8, column=0, sticky=N)
        self.textentry3 = Entry(self, textvariable=self.num3, width=20, bg="white")
        self.textentry3.grid(row=9, column=0, sticky=N)


        Label(self, text="Enter fourth number:",bg="black", fg="white", font="none 12 bold").grid(row=10, column=0, sticky=N)
        self.textentry4 = Entry(self, textvariable=self.num4, width=20, bg="white")
        self.textentry4.grid(row=11, column=0, sticky=N)


        Button(self, text="Solve", width=6, command=self.solver).grid(row=12, column=0, sticky=N)
        self.output = Text(self, width=60, height=10, wrap=WORD, background="white")
        self.output.grid(row=13, column=0, columnspan=1, sticky=N)

    def solver(self):
        target = self.target_num.get()
        number1 = self.num1.get()
        number2 = self.num2.get()
        number3 = self.num3.get()
        number4 = self.num4.get()
        numbers = [number1, number2, number3, number4]
        operators = ["+","-","*","/"]
        groups    = ['X+X+X+X', 'X+X+(X+X)', 'X+(X+X)+X', '(X+X+X)+X', '(X+X)+X+X', 'X+(X+X+X)', '((X+X)+X)+X', 'X+(X+(X+X))', 'X+((X+X)+X)', '(X+X)+(X+X)', '(X+(X+X))+X']
        seen = set()
        for values in permutations(numbers,len(numbers)):
            for operCombo in combinations_with_replacement(operators,len(numbers)-1):
                for oper in permutations(operCombo,len(numbers)-1):
                    formulaKey = "".join(str(oper+values))
                    if formulaKey in seen: continue # ignore variations on parentheses alone
                    for pattern in groups:
                        formula = "".join(str(o)+str(p) for o,p in zip([""]+list(oper), pattern.split("+")))
                        formula = "".join(str(v)+str(p) for v,p in zip([""]+list(values),formula.split("X")))
                        try:
                            if eval(formula) == target:                              
                                Answer = formula,"=",target
                                print(formula,"=",target)
                                seen.add(formulaKey)
                                #insert value in output Textbox
                                self.output.insert(INSERT, Answer) 
                                self.output.insert(END, '\n') 
                                break
                        except: pass

root = Tk()
app = Application(master=root)
app.master.title("target number solution")
app.mainloop()

答案 1 :(得分:0)

您可以更改/尝试的几件事:

  • 保持“ Entry”变量唯一。然后使用.get()命令提取每个数字。
  • 在Button的命令选项中使用lambda,如下所示:

    command = lambda:solver(num1,num2,num3,num4)

  • 如果要显示从“求解器”返回的数字,请在tkinter窗口中网格化一个Label。然后使用Label.config(text="Your number here")将其显示在tkinter窗口中