使用指针或使用放置和遗忘方法

时间:2015-08-14 05:54:43

标签: python-2.7 tkinter

我正在使用tkinter创建GUI。

在下面的代码中,我使用两种方法在GUI的屏幕上放置标签。

在方法1中,我初始化了label,然后使用label_placelabel_forge t将标签放在不同的屏幕上。

在第二种方法中,我使用指向label文本变量选项的指针,并使用set()属性将文本设置在不同的位置。

我想知道哪种方法有效。

与每个页面上的方法1一样,我必须使用forget方法,然后替换标签。

在第二种方法中,我pointer[i].set("text")进行测试,pointer[i].set(" ")没有文字。

#! /usr/bin/python
from Tkinter import *
class Gui(Frame):

    def __init__(self,parent):
        Frame.__init__(self,parent)
        self.parent=parent
        self.data_Frame()
        Label_Tuple=["a:","b:","b:","d:","a:","b:","b:","d:","a:","b:","b:","d:"]
        pos=[0,1,2,3,4,5,6,7,8,9,10,11]

        self.label_init()
        self.label_place(Label_Tuple,pos)
        #self.label_forget()

        #self.Data_Frame()
        #self.widgets_place(Label_Tuple)
        #self.widgets_forget()


    def data_Frame(self):

        self.data_frame=Frame(self.parent, bg = "cyan")
        self.data_frame.place(x=10,y=55,anchor=NW,height=370,width=510)
        self.data_frame.config(highlightbackground='black',highlightthickness='0')


    def label_init(self):

            self.label=[]

            for i in range(0,12):
                self.label.append(Label(self.data_frame,width=14,bg="cyan",anchor=NE,font=("Helvetica", 15)))


    def label_place(self,Label,position):

            self.place_x=13
            self.place_y=5
            self.incr=30
            j=0
            for i,label in enumerate(Label):

                while True:

                    if position[i]==j:
                        self.label[i].config(text=Label[i])
                        self.label[i].place(x=self.place_x,y=self.place_y)
                        break
                    else:
                        self.place_y +=self.incr
                        j +=1

    def label_forget(self):
            for i in range(0,12):
                self.para_label[i].place_forget()


    def Data_Frame(self):

        self.Data_Frame=Frame(self.parent, bg = "cyan")
        self.Data_Frame.place(x=10,y=55,anchor=NW,height=370,width=510)
        self.Data_Frame.config(highlightbackground='black',highlightthickness='0')

        self.label=[]
        place_x=13
        place_y=10
        incr=30  
        self.string_var0=[]

        for i in range(0,12):
            variable=StringVar()
            self.label.append(Label(self.Data_Frame,width=14,textvariable=variable,bg="red",anchor=NW,font=("Helvetica", 15)))
            self.string_var0.append(variable)
            self.label[i].place(x=place_x,y=place_y)
            place_y +=incr


    def widgets_place(self,Label_Tuple):

        for i in range(0,12):
            self.string_var0[i].set(Label_Tuple[i])

    def widgets_forget(self):
        Label_Tuple=["","","","","","","","","","","",""]
        for i in range(0,12):
            self.string_var0[i].set("")


if __name__== "__main__":
    root =Tk()
    #root.attributes('-fullscreen',True)
    root.geometry("750x490")
    #root.config(cursor="none")
    #bigfont = tkFont.Font(family="Helvetica",size=15,slant="roman")
    #root.option_add("*TCombobox*Listbox*Font", bigfont)
    #root.option_add("*TCombobox*Listbox*Font", bigfont)
    app=Gui(root)
    app.mainloop()

如何检查执行速度以及此python代码占用的内存或执行的代码执行时间。

1 个答案:

答案 0 :(得分:0)

我使用操作系统的时间命令。请注意,它为您提供了三个系统时间,用户时间和挂钟时间。

/ usr / bin / time -v program_name.py

您还可以使用top或htop查看内存使用情况,但程序运行时使用情况将始终发生变化。还有ps和smaps但我也没用过。

results are something like the following (note that modern systems
use as much of the CPU as is available and give up some of it when
necessary.  I am on a multi-core system so the program had
all of one core to itself).

    Command being timed: "./test_r_1.py"
    User time (seconds): 0.54
    System time (seconds): 0.28
    Percent of CPU this job got: 99%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.82
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 51248
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 37484
    Voluntary context switches: 57
    Involuntary context switches: 739
    Swaps: 0
    File system inputs: 0
    File system outputs: 176
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0