从按钮打开框架

时间:2015-05-09 08:12:15

标签: python tkinter

我有这个代码打开一个帧,计数5秒,然后打开另一个帧。在这里,我有一些按钮可以打开不同的帧,但它们都打开已经由计数器打开的相同的第二帧。

我不知道为什么会发生这种情况。 有什么想法或帮助吗?

import tkMessageBox
import MySQLdb
import sys
import datetime
from datetime import timedelta
import time
import calendar
import struct
import traceback
import uuid
import random
import hashlib
import Tkinter as tk
from Tkinter import *
from functools import partial
import ttk

LARGE_FONT= ("Verdana", 12)

start_app = time.time()

class ChangePages(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        for F in (PageSTART,PageSELECT,PageZS,PageARH,PageSET,PageHLP):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(PageSTART)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class PageSTART(tk.Frame):

    def __init__(self, parent, controller):
        self.control=controller
        tk.Frame.__init__(self, parent)

        f = Frame(self)
        f.pack(side='left')

        self.initializaresenz=Label(f,text='',font=("Arial", 18, "bold"),fg='#ff9900',width=35,height=3)
        self.initializaresenz.grid(row=4,column=0,columnspan=6)

        self.initializarestatus1=Label(f,text='Start!',font=("Arial", 18, "bold"),fg='#00cc33',width=55,height=3)
        self.initializarestatus1.grid(row=5,column=0,columnspan=6)
        self.initializarestatus2=Label(f,text='Error!',font=("Arial", 18, "bold"),fg='#ff3300',width=55,height=3)
        self.initializarestatus2.grid(row=5,column=0,columnspan=6)
        self.initializarestatus1.grid_remove()
        self.initializarestatus2.grid_remove()

        self.update_labels()

    def update_labels(self):

        now=time.time()
        diff =15 - int(now-start_app)
        txt_diff = ''

        if diff < 0:
            diff = '00'

        if diff < 10:
            txt_diff='0'+str(diff)
        else:
            txt_diff=str(diff)

        if diff < 9:
            self.initializarestatus1.grid()
            self.initializarestatus2.grid_remove()
            self.control.show_frame(PageSELECT)

        elif diff < 1:
            self.initializarestatus2.grid()
            self.initializarestatus1.grid_remove()

        self.initializaresenz.configure(text='Counting ... ' + txt_diff)

        self.after(10, self.update_labels)

class PageSELECT(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        f = Frame(self)
        f.pack(side='left')

        btnarh=Button(f,text='ARH',fg='blue',font=('Helvetica',18),height=1, width=10,command=lambda: controller.show_frame(PageZS))
        btnarh.grid(row=1,column=0)
        btnset=Button(f,text='SET',fg='blue',font=('Helvetica',18),height=1, width=10,command=lambda: controller.show_frame(PageSET))
        btnset.grid(row=1,column=1)
        btnhlp=Button(f,text='HLP',fg='blue',font=('Helvetica',18),height=1, width=10,command=lambda: controller.show_frame(PageHLP))
        btnhlp.grid(row=2,column=0)
        btnex=Button(f,text='EXIT',fg='blue',font=('Helvetica',18),height=1, width=10,command=self.exit)
        btnex.grid(row=2,column=1)

    def exit(self):
        mExit = tkMessageBox.askyesno(title='Exit', message='Exit ?')
        if mExit > 0:
            app.destroy()
            return

class PageZS(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        f = Frame(self)
        f.pack(side='left')

        labelspacing=Label(f,text='Page ZS',font=("Arial", 12, "bold"),width=35,height=0,relief='groove').grid(row=0,column=4)

class PageARH(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        f = Frame(self)
        f.pack(side='left')

        labelspacing=Label(f,text='Page ARH',font=("Arial", 18, "bold"),width=9,height=1).grid(row=0,column=0)

class PageSET(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        f = Frame(self)
        f.pack(side='left')

        labelspacing=Label(f,text='Page SET',font=("Arial", 18, "bold"),width=9,height=1).grid(row=0,column=0)


class PageHLP(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        f = Frame(self)
        f.pack(side='left')

        labelspacing=Label(f,text='Page HLP',font=("Arial", 18, "bold"),width=9,height=1).grid(row=0,column=0)


#Root loop
app = ChangePages()
app.geometry('600x400+0+0')
app.title('ASV Technologies ')
app.mainloop()

1 个答案:

答案 0 :(得分:0)

我认为没有继承会更容易,这似乎是上述程序中令人困惑的部分。您只需传递要使用的父/根窗口。通过从按钮按下调用主程序中的函数并使用该函数调用该类可能更容易理解。如果要从调用类访问第二个类,也可以在从函数调用时保存类实例。该程序多次调用一个附加类,但使用不同的按钮调用不同的类应该是一个简单的扩展。

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

from functools import partial

class OpenToplevels():
    """ open and close additional Toplevels with a button
    """
    def __init__(self):
        self.root = tk.Tk()
        but=tk.Button(self.root, text="Open a Toplevel",
                      command=partial(Another, self.root))
        but.grid(row=0, column=0)
        tk.Button(self.root, text="Exit Tkinter", bg="red",
                  command=self.root.quit).grid(row=1, column=0, sticky="we")
        self.root.mainloop()

class Another():
    def __init__(self, parent):
        self.parent=parent
        self.open_another()

    def close_it(self):
        self.id.destroy()

    def open_another(self):
        ## open a Toplevel as it is more visible as an example
        self.id = tk.Toplevel(self.parent)
        ## put something in the Toplevel
        tk.Button(self.id, text="Close Toplevel",
                  command=self.close_it,
                  bg="orange").grid(row=1, column=0)

Ot=OpenToplevels()