Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Tkinter不&';更新标签_Python_User Interface_Tkinter_Label - Fatal编程技术网

Python/Tkinter不&';更新标签

Python/Tkinter不&';更新标签,python,user-interface,tkinter,label,Python,User Interface,Tkinter,Label,我正在开发一个密码程序,使用Tkinter使它更漂亮,我有一些问题。Tkinter标签不会更新,但会在空闲状态下显示新密码。我还在学习,所以请把它放低一点。如果您需要我的源代码,请参阅: """Importations""" import os import pygame as pygame from pygame import mixer import random import string import sys from sys import platform as _platfo

我正在开发一个密码程序,使用Tkinter使它更漂亮,我有一些问题。Tkinter标签不会更新,但会在空闲状态下显示新密码。我还在学习,所以请把它放低一点。如果您需要我的源代码,请参阅:

"""Importations"""
import os

import pygame as pygame
from pygame import mixer

import random

import string

import sys
from sys import platform as _platform

import tkinter as tk
from tkinter import ttk

"""Program Definitions"""
#Color Definitions
backgroundColor = ("#001F33")

#Random
password = ("")

#Font Sizes
LARGE_FONT = ("Times", 16)
LARGE_FONT = ("Times", 14)

NORMAL_FONT = ("Times", 12)

SMALL_FONT = ("Times", 10)
XXS_FONT = ("Times", 8)

"""Various Functions"""
#Quit
def quitprog():
        sys.exit()

"""Class Setup"""
#Window Setup
class passwordGeneratorApp(tk.Tk):

    #Initialize
    def __init__(self, *args, **kwargs):

        #Container Setup
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default = "C:/Program Files/passGenerator/assets/images/programIcon.ico")
        tk.Tk.wm_title(self, "Random Password Generator")

        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        #MenuBar
        menubar = tk.Menu(container)

        #FileMenuBar
        filemenu = tk.Menu(menubar, tearoff = 1)
        filemenu.add_command(label = "Help", command = lambda: forhelp("For Help Contact the Following!"))
        filemenu.add_separator()
        filemenu.add_command(label = "Exit", command = quitprog)
        menubar.add_cascade(label = "File", state = "disabled", menu = filemenu)

        tk.Tk.config(self, menu = menubar)

        self.frames = {}

        #Pages to be Displayed
        for F in (welcomeScreen, generator):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")
            self.show_frame(welcomeScreen)

    #Show Frame
    def show_frame(self, cont):

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

"""Pages"""
#Welcome Screen
class welcomeScreen(tk.Frame):

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

        welcomeScreen.configure(self, background = backgroundColor)

        #Generator
        def generatePass():
            char_set = string.ascii_uppercase + string.digits
            password = (''.join(random.sample(char_set*6, 6)))
            print(password)

        #Openning
        text_file = open("C:/Program Files/passGenerator/assets/descriptions/welcomemsg.txt", "r")
        file = text_file.read()
        text_file.close()

        #Setups
        titleLabel = ttk.Label(self, text = "Random Password Generator", font = LARGE_FONT)
        msgWelcome = ttk.Label(self, text = file, font = NORMAL_FONT)
        generateButton = ttk.Button(self, text = "Generate", command = generatePass)
        viewButton = ttk.Button(self, text = "View Passcode", command = lambda: controller.show_frame(generator))

        #Placement
        titleLabel.pack(pady = 10)
        msgWelcome.pack()
        generateButton.pack(pady = 5)
        viewButton.pack()

#Generator
class generator(tk.Frame):

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

        generator.configure(self, background = backgroundColor)

        char_set = string.ascii_uppercase + string.digits
        password = (''.join(random.sample(char_set*6, 6)))
        print(password)

        passwordcode = ("You're password is: %s" % password)

        #Setup
        titleLabel = ttk.Label(self, text = "Password Generator", font = LARGE_FONT)
        displayButton = ttk.Button(self, text = "Display Password", command = lambda: controller.show_frame(welcomeScreen))
        passwordLabel = ttk.Label(self, text = passwordcode, font = NORMAL_FONT)


        #Placement
        titleLabel.pack(pady = 5)
        displayButton.pack()
        passwordLabel.pack(pady = 5)

pygame.mixer.init()   
app = passwordGeneratorApp()
app.geometry("1280x720")
app.mainloop()

注意:我正在Windows上运行Python 3.4。

问题是新的
生成器
框架(您应该用大写字母写类名)在这一行中只构造了一次:

frame = F(container, self)
其中
F
生成器
类。这一行在脚本的整个运行过程中被调用一次,这意味着在第一次生成密码后,该值不会被更新

您有两个选择:

  • 您可以销毁这两个帧而不是隐藏它们,并在需要显示它们时重新构造它们

  • 您可以将当前系统用于提升窗口,并更新
    passwordLabel
    小部件的值


  • 注意:导入pygame因为pygame是无用的,导入pygame就足够了