使用按钮创建新窗口

时间:2018-12-28 06:23:52

标签: python tkinter

我正在创建一个包含UI的项目,需要创建一个窗口。对于将要单击的每个按钮,应显示下一个窗口,而当前窗口消失

import tkinter
from tkinter import *
#import GUI
import sys
import tkinter
from tkinter import *
import time
import pygame

def tick():

#keydown

#def click(): 
   entered_text=textentry.get()

#CODES FOR MAIN/'GAME' WINDOW

#create window object
window = Tk()
window.title("GAME")
window.geometry('480x320')
window.resizable(False, False)

window_game = Label(window, text = "GAME")
window_game.place(width=480, height=40)

#DATE
month_text=StringVar()
day_text=StringVar()
year_text=StringVar()

month_choices = ['01','02','03','04','05','06','07','08','09','10','11','12']
month_text.set('MM')

day_choices = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
day_text.set('DD')

year_choices = ['2018','2019','2020','2021','2022','2023','2024','2025','2026','2027','2028']
year_text.set('YYYY')

def change_dd_m(*args):
    print( month_text.get() )

def change_dd_d(*args):
    print( day_text.get() )

def change_dd_y(*args):
    print( year_text.get() )

month_text.trace('w', change_dd_m)
day_text.trace('w', change_dd_d)
year_text.trace('w', change_dd_y)

date_label = Label(window, text = "DATE:")
#sep = Label(window, text= "/")
#sep2 = Label(window, text= "/")

month = OptionMenu(window, month_text, *month_choices)
day = OptionMenu(window, day_text, *day_choices)
year = OptionMenu(window, year_text, *year_choices)

date_label.place(x=90, y=50)
month.place(x=150, y=47, height=25, width=60)
#sep.place(x=150, y=40)
day.place(x=235, y=47, height=25, width=60)
#sep2.place(x=220, y=40)
year.place(x=320, y=47, height=25, width=65)

#BOARD NO
board_text=StringVar()

board_label = Label(window, text = "BOARD NO:")
board_no = Entry(window,width=2, textvariable=board_text)

board_label.place(x=90, y=80)
board_no.place(x=182, y=83, height=18, width=25)

#TIME
hour_text=StringVar()
minute_text=StringVar()

hour_choices = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23']
hour_text.set('HH')

minute_choices = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59']
minute_text.set('MM')

def change_dd_h(*args):
    print( month_text.get() )

def change_dd_m(*args):
    print( day_text.get() )

time_label = Label(window, text = "TIME:")
sep3 = Label(window, text= ":")

hour = OptionMenu(window, hour_text, *hour_choices)
minute = OptionMenu(window, minute_text, *minute_choices)

time_label.place(x=90, y=110)
hour.place(x=150, y=110, height=25, width=60)
sep3.place (x= 220, y=110)
minute.place(x=235, y=110, height=25, width=60)

#ROUND
round_text=StringVar()

round_label = Label(window, text = "ROUND:")
round_no = Entry(window,width=2, textvariable=round_text)

round_label.place(x=90, y=140)
round_no.place(x=182, y=145, height=18, width=25)

#BUTTON -> EXIT
def exit_now():
    window.destroy()

exit_bt = Button (window, text = "EXIT", command = exit_now)
exit_bt.place(x=90, y=200, height=20, width=80)

window.mainloop()

我想问一下如何关闭当前窗口并使用按钮打开新窗口。

如何使按钮转到下一个窗口并关闭创建的窗口?

1 个答案:

答案 0 :(得分:0)

如果您确实想关闭它并打开另一个,则可以将其销毁并创建一个新的。像这样:

from tkinter import *


class Window(object):
    COUNTER = 1

    def exit_now(self):
        """
        Closes the window
        """
        self.window.destroy()

    def open_new(self):
        """
        Closes the current window and creates a new one. Increases the
        number in the title to show it really happens
        """
        self.exit_now()
        self.COUNTER += 1
        self.__init__()

    def __init__(self):
        self.window = Tk()
        self.window.title(f"{self.COUNTER}")

        Button(self.window, text="EXIT", command=self.exit_now).pack()
        Button(self.window, text="NEW", command=self.open_new).pack()
        self.window.mainloop()


if __name__ == '__main__':
    window = Window()