tkinter按钮命令中的多个并行操作

时间:2017-05-14 10:42:11

标签: python-3.x tkinter

我想在tkinter窗口的一个按钮中添加并行操作,这是我的主要代码

from Fonctions import *
from time import time, sleep
listConnextion = [a,b,c,d,e,f]

MotDePasse= ""

class ConnecOptimizerApp(tk.Tk):
def __init__(self):
    tk.Tk.__init__(self)
    tk.Tk.iconbitmap(self , default= "logo.ico")
    tk.Tk.wm_title(self,"ConnecOptimizer")
    container = tk.Frame(self)
    container.pack(side = "top", fill = "both", expand = True)
    self.frames ={}
    for f in  (PageAccueil, PageAttente):
        frame = f(container,self)
        self.frames[f] = frame
        frame.grid(row = 1000, column = 500, sticky = "nsew")
    self.show_frame(PageAccueil)

def show_frame(self,cont):
    frame = self.frames[cont]
    frame.tkraise()

class PageAccueil(tk.Frame):
def __init__(self,parent,controller):
    def cocher():
        if var4 == 0:
            ip.configure(state='disabled')
        else:
            ip.configure(state='normal')
    tk.Frame.__init__(self,parent)
    Frame0 = ttk.Frame(self)
    Frame0.pack(side="top")
    Frame1 = ttk.Frame(Frame0)
    Frame1.pack(side="left", padx=70, pady=50)
    Frame2 = ttk.Frame(Frame0)
    Frame2.pack(side="left", padx=10, pady=10)
    Frame3 = ttk.Frame(self)
    Frame3.pack(side="top")
    Frame4 = ttk.Frame(Frame3)
    Frame4.pack(side = "left")
    Frame5 = ttk.Frame(Frame3)
    Frame5.pack(side = "left")
    Fr = ttk.Frame(self).pack(side = "top")
    Frame6 = ttk.Frame(self)
    Frame6.pack(side = "top")
    Frame7 = ttk.Frame(Frame6)
    Frame7.pack(side="left")
    Frame8 = ttk.Frame(Frame6)
    Frame8.pack(side="left")
    Frame9 = ttk.Frame(self)
    Fr2 = ttk.Frame(self).pack(side = "top")
    Frame9.pack(side = "top")
    Frame10 = ttk.Frame(Frame9)
    Frame10.pack(side = "left")
    Frame11 = ttk.Frame(Frame9)
    Frame11.pack(side = "left")
    Frame12 = ttk.Frame(self)
    Frame12.pack(side = "top")
    varGr = tk.StringVar()
    varGr.set(0)
    for i in range(len(listConnextion)):
        b = ttk.Radiobutton(Frame1, variable=varGr, text=listConnextion[i], value=i)
        b.grid(row=i , sticky = "w")
    var1 = tk.IntVar()
    ttk.Checkbutton(Frame2, text="Graphique", variable=var1).grid(row=1 ,sticky = "w" )
    var2 = tk.IntVar()
    ttk.Checkbutton(Frame2, text="Diagnostic", variable=var2).grid(row=2 , sticky = "w")
    var3 = tk.IntVar()
    ttk.Checkbutton(Frame2, text="Optimisation", variable=var3).grid(row=3 , sticky = "w")
    lab = ttk.Label(Frame4, text = "Periode de reference de l'historique en jour ")
    lab.pack(side = "left" , padx = 30 )
    nbJour = ttk.Entry(Frame5)
    nbJour.pack(side = "left" )
    l = ttk.Label(Fr).pack()
    lab2 = ttk.Label(Frame7, text = "Nombre d'entrées pour finir l'apprentissage")
    lab2.pack(side = "left" , padx = 30)
    nbEntre = ttk.Entry(Frame8)
    nbEntre.pack(side = "left" )
    l2 = ttk.Label(Fr2).pack()
    var4 = tk.IntVar()
    ttk.Checkbutton(Frame10 , text = "Iperf", variable = var4 , command = cocher).pack( padx = 10)
    ip = ttk.Entry(Frame11 , state = 'disabled')
    ip.pack(padx = 20)
    B = ttk.Button(Frame12, text="Valider" , command = (lambda: controller.show_frame(PageAttente)))
    B.pack(side = "top" , pady = 30)

class PageAttente(tk.Frame):
def __init__(self,parent,controller):
    tk.Frame.__init__(self,parent)
    frame0 = ttk.Frame(self)
    frame0.pack(side = "top")
    frame1 = ttk.Frame(self)
    frame1.pack(side = "top")
    self.T = tk.Text(frame0, height=20, width=53)
    self.S = ttk.Scrollbar(frame0)
    self.T.config(yscrollcommand=self.S.set)
    self.T.pack(side=tk.LEFT, fill=tk.Y)
    self.S.config(command=self.T.yview)
    self.S.pack(side=tk.RIGHT, fill=tk.Y)
    self.updateWidgets()
    ttk.Button(frame1, text = "Arreter Diagnostic", command = (lambda : controller.show_frame(PageAccueil))  ).pack(pady = 10)

def updateWidgets(self):
    with open('text.txt') as f:
        newText = f.read()
    self.T.delete('1.0', tk.END)
    self.T.insert(tk.END, newText)
    self.after(1000, self.updateWidgets)


app = ConnecOptimizerApp()
app.geometry("450x400+300+140")
app.mainloop() 

在这里你可以找到我的功能

import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style

def PageLogin():
root = tk.Tk()
root.geometry("350x130+530+280")
root.title("Super-utilisateur")
def leave():
    MotDePasse = champMp.get()
    print(MotDePasse)
    root.destroy()
topFrame = ttk.Frame(root)
topFrame.pack()
middleFrame = ttk.Frame(root)
middleFrame.pack()
bottomFrame = ttk.Frame(root)
bottomFrame.pack()
lab1 = ttk.Label(topFrame, text=" ").pack()
lab2 = ttk.Label(topFrame, text="Veuillez entrer le mot de passe super-utilisateur").pack()
labelMessage2 = ttk.Label(middleFrame, text="mot de passe ")
labelMessage2.pack(side="left", padx=10, pady=10)
champMp = ttk.Entry(middleFrame, show='*')
champMp.pack(side="right", padx=10, pady=10)
b = ttk.Button(bottomFrame, text="valider", command = leave)
b.grid(row=4, pady=5, sticky="w")
root.mainloop()

def TracerGraphe ():
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax2 = fig.add_subplot(1, 1, 1)
ax3 = fig.add_subplot(1, 1, 1)

def animate(i):
    graph_data = open('test.txt', 'r').read()
    lines = graph_data.split('\n')
    xs = []
    ys = []
    zs = []
    ws = []
    for line in lines:
        if len(line) > 1:
            x, y, z, w = line.split(',')
            xs.append(x)
            ys.append(y)
            zs.append(z)
            ws.append(w)
    ax1.clear()
    ax2.clear()
    ax3.clear()
    ax1.plot(xs, ys, label='RTT')
    ax2.plot(xs, zs, label='Debit')
    ax3.plot(xs, ws, label='taux d erreur')
    ax1.legend()
    ax2.legend()
    ax3.legend()

ani = animation.FuncAnimation(fig, animate, interval=1000)
mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(700, 100, 640, 545)
plt.show()

我不能理解:当我按下B按钮时我想使用“lambda:controller.show_frame(PageAttente)”显示PageAttente,使用“PageLogin()”函数显示LoginPage并使用“TracerGraphe”显示图形)“

我已经尝试过我在网上找到的所有解决方案,但它不起作用

有人可以帮我吗?

谢谢

0 个答案:

没有答案