2

我正在尝试创建一个允许用户通过条目小部件输入“用户名”的游戏。撇开整个游戏方面不谈,现在我只需要一个加载标题屏幕的程序,然后单击一个按钮会出现一个新的“设置”窗口,提示我输入我的用户名,然后单击一个按钮,设置窗口就会出现离开,我有我的游戏窗口,标签上写着“用户名:[你的名字]”。

谁能帮我这个?

(编辑:这是我目前正在处理的代码

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

抱歉,代码是西班牙语的一半,但您仍然应该了解正在发生的事情)。

4

1 回答 1

0

我希望这段代码能够帮助你弄清楚如何实现弹出文本对话框。这个例子的工作方式是在主窗口(根)中实现一个按钮,当你点击它时,弹出对话框会出现MyDialog类对象,将被创建,然后它将使用wait_window()等待直到它完成。你可以看到弹出对话框是如何实现的,它是一个简单的TopLevel小部件,它就像你能想到的另一个框架,你打包你的标签,你的Entry字段,最后是一个按钮mySubmitButton

向下看send函数,它几乎只是使用简单.get()的方法从Entry. 然后您将destory()打开窗口,然后恢复主窗口。您可以根据需要多次执行此操作。

import tkinter as tk

class MyDialog:
    def __init__(self, parent):
        top = self.top = tk.Toplevel(parent)
        self.myLabel = tk.Label(top, text='Enter your username below')
        self.myLabel.pack()

        self.myEntryBox = tk.Entry(top)
        self.myEntryBox.pack()
        self.mySubmitButton = tk.Button(top, text='Submit', command=self.send)
        self.mySubmitButton.pack()

    def send(self):
        global username
        username = self.myEntryBox.get()
        self.top.destroy()

def onClick():
    inputDialog = MyDialog(root)
    root.wait_window(inputDialog.top)
    print('Username: ', username)

username = 'Empty'
root = tk.Tk()
mainLabel = tk.Label(root, text='Example for pop up input box')
mainLabel.pack()

mainButton = tk.Button(root, text='Click me', command=onClick)
mainButton.pack()

root.mainloop()

在此处输入图像描述

在示例输出中,我做了两次。输入GeorgeSam(我的朋友)作为用户名,每次打开新对话框时都会更新用户名。输出:

>>> ================================ RESTART ================================
>>> 
Username:  George
Username:  Sam

编辑:提交按钮似乎有问题?它有时不想出现。它是可点击的,但在被点击之前它不会出现。这似乎是 Mac OS 的唯一问题。

使用的参考:effbotstackoverflowtutorialspointdaniweb

于 2012-04-07T04:49:08.810 回答