tkinter:下拉菜单无法点击

时间:2016-03-02 05:06:28

标签: python tkinter

我正在创建一个tkinter窗口,每秒更新一次矩阵乘法结果。我希望使用下拉菜单为用户创建选项。 但是,由于整个窗口每秒重新绘制一次以进行矩阵更新,因此下拉菜单每秒重新初始化,使它们几乎无用。

我想知道是否有任何办法解决这个问题。

提前致谢。

from tkinter import *
import time
import sys 

def update(a):

    root.title("Matrix Multiplication")
    menu = Menu(root) 
    root.config(menu= menu) 
    subMenu = Menu(menu)  
    menu.add_cascade(label="Options", menu=subMenu)
    subMenu.add_command(label="Opt1...",command=win2)

    exitMenu = Menu(menu)
    menu.add_cascade(label="Exit", menu=exitMenu)
    exitMenu.add_command(label="Exit",command=root.destroy)

    X0 = [[8,7,3],[4 ,5,6],[7 ,8,9]]
    Y0 = [[5,8,1,2],[6,7,3,0],[4,5,9,1]]
    result0 = [[0,0,0,0],[0,0,0,0],[0,0,0,0]]

    if a == 0:
        cpfg = ["magenta", "blue", "green", "purple"]
        cpbg = ["white", "white", "white", "white"]

        button1 = Button(root, text="Button-1", fg=cpfg[0], bg=cpbg[0])
        button2 = Button(root, text="Button-2", fg=cpfg[1], bg=cpbg[1])
        button3 = Button(root, text="Button-3", fg=cpfg[2], bg=cpbg[2])
        button4 = Button(root, text="Button-4", fg=cpfg[3], bg=cpbg[3])

        button1.grid(row=0, column=0)
        button2.grid(row=0, column=1)
        button3.grid(row=0, column=2)
        button4.grid(row=0, column=3)

        a = 1

    elif a >= 1 and a <= 3:

        for b in range(len(X0)):
            for c in range(len(X0[0])):
                X0[b][c] *= a
        a += 1

    else:

        for b in range(len(X0)):
            for c in range(len(X0[0])):
                X0[b][c] *= a
        a = 1

    for i in range(len(X0)):
        for j in range(len(Y0[0])):
            for k in range(len(Y0)):
                 result0[i][j] += X0[i][k] * Y0[k][j]

    agrp = LabelFrame(root, text="Process-0", padx=5, pady=5)
    agrp.grid(row=2, column=1)

    for r in range(3):
        for c in range(4):
            Label(agrp, text=result0[r][c],             
                borderwidth=4 ).grid(row=r,column=c)

    root.after(1000, lambda x = a: update(x))

def win2():
    board = Toplevel()
    board.title("Message")
    S = Scrollbar(board)
    T = Text(board, height=4, width=50)
    T.pack()
    S.pack(side=RIGHT, fill=Y)
    T.pack(side=LEFT, fill=Y)
    S.config(command=T.yview)
    T.config(yscrollcommand=S.set)
    quote = """Yep, this is text"""
    T.insert(END, quote)


root = Tk()
a=0
update(a)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

update方法应该只计算和显示更改。

def update(a):
    if a == 0:
        a = 1

    elif a >= 1 and a <= 3:
        for b in range(len(X0)):
            for c in range(len(X0[0])):
                X0[b][c] *= a
        a += 1

    else:
        for b in range(len(X0)):
            for c in range(len(X0[0])):
                X0[b][c] *= a
        a = 1

    for i in range(len(X0)):
        for j in range(len(Y0[0])):
            for k in range(len(Y0)):
                 result0[i][j] += X0[i][k] * Y0[k][j]

    for r in range(3):
        for c in range(4):
            Label(agrp, text=result0[r][c],             
                borderwidth=4 ).grid(row=r,column=c)

    root.after(1000, lambda x = a: update(x))