繁体   English   中英

Python / Tkinter不会更新标签

[英]Python/Tkinter doesn't update label

我正在开发使用Tkinter使其变得更好的密码程序,并且遇到了一些问题。 Tkinter标签不会更新,但是会在IDLE中显示新密码。 我还在学习,所以请把它弄一点。 如果您需要我的源代码,则为:

"""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。

问题是新的generator框架(您应该在大写btw中写类名)在此行中仅构建一次:

frame = F(container, self)

其中Fgenerator类。 在脚本的整个运行过程中都会调用此行一次,这意味着在第一次生成密码后不会更新该值。

您有两种选择:

  1. 您可以销毁这两个框架而不是隐藏它们,而仅在需要显示它们时再次构造它们。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM