【问题标题】:How to remove the widgets and replace them in python如何删除小部件并在python中替换它们
【发布时间】:2019-03-29 07:47:39
【问题描述】:

我是 tkinter 的新手,想将我制作的一段已经存在的代码更改为 GUI。下面的代码是一个用户名和密码系统。我需要帮助的是我无法弄清楚如何获得新盒子或删除 gui 的小部件。下面的代码没有任何问题,但我想向您展示,因为它向您展示了我是如何编码的,以及如何根据此代码制作一个新盒子。 顺便说一句,我在 python 3.5.1 和 Windows 10 上。

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')
    else:
        box.showinfo('info','Invalid Login')



window = Tk()
window.title('Music quiz')
window.geometry("300x125")
window.wm_iconbitmap('Favicon.ico')

frame = Frame(window)

Label1 = Label(window,text = 'Username:')
Label1.pack()

entry1 = Entry()
entry1.pack()



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

entry2 = Entry()
entry2.pack()

【问题讨论】:

  • 请澄清您所说的“新盒子”是什么意思
  • 基本上我想要的是删除小部件或您使用用户名和密码文本制作的实际框,这样我就可以用它进行更多的 GUI 编码。这是一个基本问题,但我对修补程序绝对是个菜鸟,所以我不知道该怎么做。

标签: python windows tkinter


【解决方案1】:

这是经过编辑的代码,我认为它可以满足您的要求。解释在cmets形式的代码中。

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

【讨论】:

  • 这正是我所需要的,它可以工作,让我可以继续编写我的游戏。谢谢
猜你喜欢
  • 2022-12-12
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多