简体   繁体   中英

Python/Tkinter doesn't update label

I am developing a password program that uses Tkinter to make it nicer and I am having some issues. The Tkinter label does not update, yet it displays the new password in the IDLE. I am still learning so please dumb it down a little bit. If you need my source code, here it is:

"""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()

NOTE: I am running Python 3.4 on Windows.

The problem is that the new generator frame (you should write class names in capital btw) is only constructed once in this line:

frame = F(container, self)

where F is the generator class. This line is called once in the entire run of your script, meaning the value not gettin updated after the first password generation.

You have two options:

  1. You can destroy the two frames instead of hiding them and just construct them again when they need to be displayed.

  2. You can use your current system with raising windows and update the values of the passwordLabel widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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