如何删除小部件并在python中替换它们

时间:2018-10-24 19:37:02

标签: python windows tkinter

我是tkinter的新手,并且想将我编写的已经存在的代码更改为GUI。下面的代码是用户名和密码系统。我需要帮助的是,我无法弄清楚如何获取新框或删除GUI的小部件。下面的代码没什么错,但是我想向您展示一下,因为它展示了我如何编码以及如何基于此代码制作一个新盒子。 顺便说一句,我在python 3.5.1和Windows 10中。

import numpy as np
import matplotlib.pyplot as plt

degrees = np.random.randint(0, 360, size=200)
radians = np.deg2rad(degrees)

bin_size = 20
a , b=np.histogram(degrees, bins=np.arange(0, 360+bin_size, bin_size))
centers = np.deg2rad(np.ediff1d(b)//2 + b[:-1])

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection='polar')
ax.bar(centers, a, width=np.deg2rad(bin_size), bottom=0.0, color='.8', edgecolor='k')
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
plt.show()

1 个答案:

答案 0 :(得分:1)

在这里,我认为编辑后的代码可以满足您的要求。在代码中以注释的形式进行解释。

import tkinter
from tkinter import *
import tkinter.messagebox as box
import time

def dialog1():
    username=entry1.get()
    password = entry2.get()
    if (username == 'Noel' and  password == 'Music quiz'):
        box.showinfo('info','You may now enter the Music quiz')
        loginframe.destroy()  #remove the login frame
        ##code to create the quiz goes here##
    else:
        box.showinfo('info','Invalid Login')




window = Tk()
window.title('Music quiz')
window.geometry("300x125")
window.wm_iconbitmap('Favicon.ico')
loginframe = Frame(window)  #create an empty frame for login
loginframe.pack()  #put the empty frame into the window


#all elements below are put into the 'loginframe' Frame
Label1 = Label(loginframe,text = 'Username:')
Label1.pack()

entry1 = Entry(loginframe)
entry1.pack()

Label2 = Label(loginframe,text = 'Password: ')
Label2.pack()

entry2 = Entry(loginframe)
entry2.pack()

donebttn = Button(loginframe, text='Done',
                  command=dialog1)  #create a button to continue
donebttn.pack()  #display that button
mainloop()