2017-02-03 4 views
0

Ich möchte nur das "Menü" -Fenster am Anfang des Programms laufen lassen und das "Board" -Fenster öffnen, wenn das "PLAY!" Die Taste wird gedrückt, aber das Fenster ist bereits vorhanden, wenn das Programm gestartet wird. Durch Drücken der Wiedergabetaste werden alle Funktionen innerhalb der Funktion gespeichert.Tkinter Fenster geöffnet hinter nur Fenster läuft in Hauptschleife

from Tkinter import * 
menuw = Tk() 
boardw = Tk() 
menuw.title("menu") 
menuw.configure(bg="ivory") 
boardw.title("Treasure Hunt!") 
boardw.configure(bg="ivory") 

rows = 8 #sets the number of rows in the grid 
columns = 8 # sets the number of columns in the grid 
size = 75 #sets the size of each square 
colour1 = "white" #sets the colour of half of the squares 
colour2 = "black" #sets the colour of the other half of the squares 
canvas_width = columns * size 
canvas_height = rows * size 

def Board(): 
    rows = 8 
    columns = 8 
    size = 50 
    colour1 = "white" 
    colour2 = "black" 
    canvas_width = columns * size 
    canvas_height = rows * size 
    Frame(boardw) 
    global canvas 
    canvas = Canvas(boardw, borderwidth=0, highlightthickness=0, width=canvas_width, height=canvas_height, background="ivory") 
    canvas.pack(side="top", fill="both", expand = True, padx=2, pady=2) 
    canvas.bind("<Configure>", refresh) 
    canvas1 = Canvas(boardw, borderwidth=0, highlightthickness=0, width=canvas_width, height=20, background="ivory") 
    canvas1.pack(side = "bottom", fill= "both", expand = True, padx=4, pady=4) 
    gold = 0 
    score = Label(boardw, text = ("score = {0}").format(gold), bg="ivory", font = "haettenschweiler 15") 
    score.pack() 
    treasurechests = 10 
    tcn = Label(boardw, text = ("Number of treasure chests remaining = {0}").format(treasurechests), bg="ivory", font = "haettenschweiler 15") 
    tcn.pack() 
    bandits = 5 
    bn = Label(boardw, text = ("Number of bandits chests remaining = {0}").format(bandits), bg="ivory", font = "haettenschweiler 15") 
    bn.pack() 
    playerpos = [0,0] 
    pos = Label(boardw, text = ("position = {0}").format(playerpos), bg="ivory", font = "haettenschweiler 15") 
    pos.pack() 

def refresh(event): 
    xsize = int((event.width-1)/columns) 
    ysize = int((event.height-1)/rows) 
    size = min(xsize, ysize) 
    canvas.delete("square") 
    colour = colour2 
    for row in range(rows): 
     colour = colour1 if colour == colour2 else colour2 
     for col in range(columns): 
      x1 = (col * size) 
      y1 = (row * size) 
      x2 = x1 + size 
      y2 = y1 + size 
      canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=colour, tags="square") 
      colour = colour1 if colour == colour2 else colour2 
    canvas.tag_raise("piece") 
    canvas.tag_lower("square") 
    canvas.pack(side = "top", fill= "both", expand = True, padx=4,pady=4) 

def menu(): 
    titlel = Label(menuw, text = "Treasure Hunt!", font = "Haettenschweiler 50", fg = "black", bg= "ivory") 
    titlel.pack() 
    playb = Button(menuw, text = "PLAY", font = "Haettenschweiler 15", fg = "black", bg= "ivory", command = Board) 
    playb.pack() 
    quitb = Button(menuw, text = "QUIT", font = "Haettenschweiler 15", fg = "black", bg= "ivory", command = menuw.destroy) 
    quitb.pack() 

menu() 
menuw.mainloop() 

Antwort

1

Dies geschieht, weil Sie zwei Instanzen von Tkinter schaffen:

menuw = Tk() 
boardw = Tk() 

In fast allen Fällen werden Sie nicht tun wollen, stattdessen ein neues Fenster zu erstellen, verwenden Sie Toplevel() für dein Board und ermöglichen menuw das root-Fenster zu sein:

from Tkinter import * 
menuw = Tk() 
menuw.title("menu") 
menuw.configure(bg="ivory") 

.... 

def Board(): 
    boardw = Toplevel() 
    boardw.title("Treasure Hunt!") 
    boardw.configure(bg="ivory") 
    .... 

auch diese Zeile: Frame(boardw) ist nichts zu tun, da Sie es nie zu einem la geschickt yout manager (pack, place, grid) um es anzuzeigen.