我正在尝试创建一个允许用户通过条目小部件输入“用户名”的游戏。撇开整个游戏方面不谈,现在我只需要一个加载标题屏幕的程序,然后单击一个按钮会出现一个新的“设置”窗口,提示我输入我的用户名,然后单击一个按钮,设置窗口就会出现离开,我有我的游戏窗口,标签上写着“用户名:[你的名字]”。
谁能帮我这个?
(编辑:这是我目前正在处理的代码
from tkinter import *
import random
import os
import time
import math
def cargarimagen(nombre):
ruta=os.path.join('Images',nombre)
imagen=PhotoImage(file=ruta)
return imagen
def operacion():
num1=random.randint(0,255)
num2=random.randint(0,255)
op=random.randint(0,4)
if op == 0:
prompt= (hex(num1)[2:].upper() + " + " + hex(num2)[2:].upper())
answer= num1 + num2
if op == 1:
prompt= (hex(num1)[2:].upper() + " - " + hex(num2)[2:].upper())
answer= num1 - num2
if op == 2:
prompt= (hex(num1)[2:].upper() + " C16")
answer= (16**2) - num1
if op == 3:
prompt= (hex(num1)[2:].upper() + " C15")
answer= (16**2) - num1 - 1
return prompt
def changeName():
name = setup.yourName.get()
GameWindow.labelText.set(name)
setup.yourName.delete(0, END)
setup.yourName.insert(0, ' ')
return
def play():
return GameWindow() + changeName()
root= Tk()
root.title('Polyominos Start Menu')
root.minsize(640,480)
root.maxsize(640,480)
class MainWindow:
def __init__(self,parent):
self.contenedor=Canvas(parent,width=640,height=480)
self.contenedor.place(x=0,y=0)
self.foto=cargarimagen('Tetris.gif')
self.label_fondo=Label(self.contenedor,image=self.foto)
self.label_fondo.place(x=0,y=0)
self.but=Button(root, text="START",font="Helvetica",width=10,height=2,bg='White',command=SetupWindow)
self.but.place(x=213,y=413)
self.about=Button(root, text="ABOUT",font="Helvetica",width=10,height=2,bg='White',command=AboutWindow)
self.about.place(x=320,y=413)
def SetupWindow():
setup=Toplevel()
setup.minsize(124,190)
setup.maxsize(124,190)
setup.title('Set Up')
frame = Frame(setup,bg ="White" )
frame.grid()
lab1= Label(frame, text='Enter your name:', bg ="White").grid(column=0, row=0)
custName = StringVar(None)
yourName = Entry(frame, textvariable=custName, bg ="White").grid(column=0, row=1)
lab2= Label(frame, text=' ', bg ="White").grid(column=0, row=2)
lab3= Label(frame, text='Pick a difficulty:', bg ="White").grid(column=0, row=3)
diff = StringVar()
diff.set(None)
radio1 = Radiobutton(frame, text='Easy', value='Easy', variable = diff, bg ="White").grid(column=0,row=4)
radio1 = Radiobutton(frame, text='Medium', value='Medium', variable = diff, bg ="White").grid(column=0,row=5)
radio1 = Radiobutton(frame, text='Hard', value='Hard', variable = diff, bg ="White").grid(column=0,row=6)
lab4= Label(frame, text=' ', bg ="White").grid(column=0, row=7)
playnow= Button(frame, text='READY', command=play, bg ="White").grid(column=0,row=8)
lab5= Label(frame, text=' ', bg ="White").grid(column=0, row=9)
def GameWindow():
root.withdraw()
GameWindow=Toplevel()
GameWindow.minsize(595,300)
GameWindow.maxsize(595,300)
GameWindow.title('Polyominos')
operaciones=Frame(GameWindow,width=300,height=300, bg='white', highlightthickness=5, highlightbackground='black')
operaciones.place(x=0, y=0)
labelText = StringVar()
labelText.set('USER')
usrname = Label(operaciones, textvariable=labelText)
usrname.place(x=80, y=20)
cubos=Frame(GameWindow,width=300,height=300, bg="white", highlightthickness=5, highlightbackground='black')
cubos.place(x=295, y=0)
def AboutWindow():
about=Toplevel()
about.minsize(137,94)
about.maxsize(137,94)
about.title('About Polyominos')
frame1 = Frame(about,bg ="White" )
frame1.grid()
lab0 = Label(frame1, text="David Salazar Quintana",bg ="White").grid(column=0,row=0)
lab1 = Label(frame1, text="ITCR",bg ="White",).grid(column=0,row=1)
lab2 = Label(frame1, text="Ingeneria en computadores",bg ="White").grid(column=0,row=2)
lab3 = Label(frame1, text="Profesor: Milton Villegas",bg ="White").grid(column=0,row=3)
lab4 = Label(frame1, text="Version: 1.0 (Abril 2012)",bg ="White").grid(column=0,row=4)
Botones_Principales=MainWindow(root)
mainloop()
root.destroy()
抱歉,代码是西班牙语的一半,但您仍然应该了解正在发生的事情)。