我正在开发一个密码程序,它使用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。
答案 0 :(得分:0)
问题是新的generator
框架(你应该用大写btw编写类名)只在这一行构建一次:
frame = F(container, self)
其中F
是generator
类。在整个脚本运行中调用此行一次,这意味着在第一次生成密码后,该值不会更新。
您有两种选择:
您可以销毁这两个框架而不是隐藏它们,只需在需要显示时再构建它们。
您可以使用当前系统来提升窗口并更新passwordLabel
小部件的值。