为什么主窗口第三帧中的开始按钮不执行计数器功能?

时间:2016-05-31 06:56:42

标签: python-3.x tkinter

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

创建三个“窗口”,用户可以使用“后退”和“下一步”按钮进行导航。

from tkinter import *
import tkinter as tk

创建计数器

def a():    


    def counter_label(label):

        counter=0
        def count():

            global counter
            counter += 1
            label.config(text=str(counter))
            label.after(1000,count)
        count()
    label=tk.Label(third_frame,fg="red")
    label.grid(row=5,column=1)





def create_widgets_in_first_frame():

为框架

创建标签
    Label(first_frame,text="DATALOG CONTROLS AND SOLUTIONS PRIVATE LIMITED",padx=25,font=("arial",25,"bold"),bg="violet").grid(row=0,column=2,sticky=W)

    # Creating the button for the frame

    Button(first_frame,text="CONFIG",padx=25,pady=70,font=("arial",20,"bold"),bg="red",command = call_second_frame_on_top,width=10).grid(row=1,column=0,sticky=W)
    Button(first_frame,text="TEST",padx=25,pady=70,font=("arial",20,"bold"),bg="red",command=call_third_frame_on_top,width=10).grid(row=2,column=0,sticky=W)
    Button(first_frame,text="EXIT",padx=25,pady=70,font=("arial",20,"bold"),bg="red",command=root_window.destroy,width=10).grid(row=3,column=0,sticky=W)

def create_widgets_in_second_frame():

为框架

创建标签
    Label(second_frame,text="SET CURRENT",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=1,column=0,sticky=W)
    Label(second_frame,text="SET VOLTAGE",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=2,column=0,sticky=W)
    Label(second_frame,text="SET TIME",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=3,column=0,sticky=W)


    def printVal():

在第二帧中创建条目并在第三帧中打印存储的值

        string1 = entryval1.get()
        string2 = entryval2.get()
        string3 = entryval3.get()
        Label(third_frame,text=string1).grid(row=1,column=1)
        Label(third_frame,text=string2).grid(row=1,column=3)
        Label(third_frame,text=string3).grid(row=1,column=5)

    entryval1 = Entry(second_frame)
    entryval1.grid(row = 1, column = 1)
    entryval2 = Entry(second_frame)
    entryval2.grid(row = 2, column = 1)
    entryval3 = Entry(second_frame)
    entryval3.grid(row = 3, column = 1)

为框架

创建按钮
    Button(second_frame,text="SAVE",padx=25,pady=50,font=("arial",20,"bold"),bg="red",command=printVal,width=10).grid(row=5,column=1,sticky=W)
    Button(second_frame,text="BACK",padx=25,pady=50,font=("arial",20,"bold"),command = call_first_frame_on_top,bg="red",width=10).grid(row=5,column=2,sticky=E)
    Button(second_frame,text="NEXT",padx=25,pady=50,font=("arial",20,"bold"),command = call_third_frame_on_top,bg="red",width=10).grid(row=5,column=3,sticky=E)

def create_widgets_in_third_frame():

为框架

创建标签
    Label(third_frame,text="SET CURRENT",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=1,column=0,sticky=W)
    Label(third_frame,text="SET VOLTAGE",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=1,column=2,sticky=W)
    Label(third_frame,text="SET TIME",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=1,column=4,sticky=W)

    Label(third_frame,text="CURRENT:",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=3,column=0,sticky=W)
    Label(third_frame,text="VOLTAGE:",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=4,column=0,sticky=W)
    Label(third_frame,text="TIME:",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=5,column=0,sticky=W)

为框架

创建按钮
    Button(third_frame,text="START",padx=25,pady=50,font=("arial",20,"bold"),command=a,bg="red",width=10).grid(row=6,column=2,sticky=E)
    Button(third_frame,text="STOP",padx=25,pady=50,font=("arial",20,"bold"),bg="red",width=10).grid(row=6,column=3,sticky=E)
    Button(third_frame,text="BACK",padx=25,pady=50,font=("arial",20,"bold"),command = call_second_frame_on_top,bg="red",width=10).grid(row=6,column=4,sticky=E)
    Button(third_frame,text="EXIT",padx=25,pady=50,font=("arial",20,"bold"),command = quit_program,bg="red",width=10).grid(row=6,column=5,sticky=E)

创建框架

的条目
    e1=Entry(third_frame,width=20)
    e1.grid(row=3,column=1)
    e2=Entry(third_frame,width=20)
    e2.grid(row=4,column=1)


def call_first_frame_on_top():

只能从第二个窗口调用此函数。

隐藏第二个窗口并显示第一个窗口。

    second_frame.grid_forget()
    first_frame.grid(column=0, row=0, padx=20, pady=5, sticky=W)

def call_second_frame_on_top():

可以从第一个和第三个窗口调用此函数。

隐藏第一个和第三个窗口并显示第二个窗口。

    first_frame.grid_forget()
    third_frame.grid_forget()
    second_frame.grid(column=0, row=0, padx=20, pady=5, sticky=W)

def call_third_frame_on_top():

只能从第二个窗口调用此函数。

隐藏第二个窗口并显示第三个窗口。

    second_frame.grid_forget()
    third_frame.grid(column=0, row=0, padx=20, pady=5, sticky=W)

def quit_program():
root_window.destroy()

 ###############################
 # Main program starts here :) #
 ###############################

# Creating the root GUI window.

root_window=Tk()
root_window.geometry("1500x1000+0+0")

定义窗口大小

window_width = 1500
window_heigth = 1000

在根窗口内创建框架以保存其他GUI元素。必须在主程序中创建所有帧,否则无法在函数中访问它们。

first_frame=Frame(root_window,bg="blue", width=window_width, height=window_heigth)
first_frame.grid(column=0, row=0, padx=20, pady=5, sticky=W)

second_frame=Frame(root_window,bg="blue", width=window_width, height=window_heigth)
second_frame.grid(column=0, row=0, padx=20, pady=5, sticky=W)

third_frame=Frame(root_window,bg="blue", width=window_width, height=window_heigth)
third_frame.grid(column=0, row=0, padx=20, pady=5, sticky=W)

为所有框架创建所有小部件

create_widgets_in_third_frame()
create_widgets_in_second_frame()
create_widgets_in_first_frame()

以相反的顺序隐藏所有帧,但保留第一帧可见(未隐藏)。

third_frame.grid_forget()
second_frame.grid_forget()

更改主窗口的背景颜色

root_window.configure(background="blue")

启动tkinter事件 - 循环

root_window.mainloop()

0 个答案:

没有答案