2017-03-10 5 views
0

Ich bin neu bei Python und Tkinter und habe den Fehler unten immer angezeigt, wenn ich versuche, benutzerdefinierte Rastergrößen hinzuzufügen mein Programm. Ich bin mir ziemlich sicher, dass dies kein doppelter Thread ist wie in allen anderen Threads, keiner von ihnen beinhaltet ein Raster oder nutzt die Rasterfunktion in ihrem Problem.TypeError: nicht unterstützte Operandentypen für /: 'int' und 'str' beim Erstellen von Gittern

Der Fehler, den ich bekomme, tritt auf, wenn ich versuche, das vale der GridSize innerhalb der GUI zu ändern. Sie können den Wert davon im "Ändern Sie die Einstellungen der Schatzsuche Spiel" Teil des Menüs ändern. Ich bin neu bei Python, also stellen Sie es bitte so einfach wie möglich dar!

Hier ist mein Code:

import tkinter 
import math 
import random 
from tkinter import * 
import tkinter as tk 
from tkinter import ttk 



GridSizeSetByUser = '8x8' 
choicesRows = ['8', '10', '12', '14'] 
v = choicesRows[0] 
choicesColumns = ['8', '10', '12', '14'] 
v2 = choicesColumns[0] 

GridRows = 9 
GridColumns = 9 


def getRows(): 
    global GridRows 
    GridRows = GridRowSpinbox.get() 
    print(GridRows) 

def getColumns(): 
    global GridColumns 
    GridColumns = GridColumnSpinbox.get() 
    print(GridColumns) 

def Treasure_Hunt_Window(): 
    THunt = tk.Tk() 
    THunt.title("Treasure Hunt") 
    THuntInstructions = "Find the treasure hidden deep in the sand!Use ye arrow keys to move around,\n\n then press Space to search that spot! Keep searching until ye find it!" 
    board = GameBoard(THunt) 
    board.pack(side="top", fill="both", expand="true", padx=4, pady=4) 
    THunt.mainloop() 

def Settings_Window(): 
    Settings = tk.Tk() 
    Settings.title("Settings") 
    SettingsWelcome = tk.Label(Settings, text='Settings Menu', width=50, fg="magenta") 
    SettingsGridSize = tk.Label(Settings, text='Grid Size:', width =50, fg="magenta") 
    global mystring 
    mystring = StringVar() 
    global mystring2 
    mystring2 = StringVar() 
    global GridRowSpinbox 
    GridRowSpinbox = Spinbox(Settings, values=choicesRows, textvariable=mystring, width=50, state="readonly", fg="magenta") 
    SaveRowSize = tk.Button(Settings, text='Save row size for grid', width=50, fg="magenta", command = getRows) 
    global GridColumnSpinbox 
    GridColumnSpinbox = Spinbox(Settings, values=choicesColumns, textvariable=mystring2, state="readonly", width=50, fg="magenta") 
    SaveColumnSize = tk.Button(Settings, text='Save column size for grid', width=50, fg="magenta", command = getColumns) 
    SettingsBandits = tk.Label(Settings, text='Amount of Bandits:', width =50, fg="magenta") 
    BanditAmount = tk.Entry(Settings, width = 50, fg="magenta") 
    SettingsBandits = tk.Label(Settings, text='Amount of Treasure Chests (up to 64)', width =50, fg="magenta") 
    SettingsWelcome.pack(fill=X) 
    SettingsGridSize.pack(fill=X) 
    GridRowSpinbox.pack(fill=X) 
    SaveRowSize.pack(fill=X) 
    GridColumnSpinbox.pack(fill=X) 
    SaveColumnSize.pack(fill=X) 
    SettingsBandits.pack(fill=X) 
    BanditAmount.pack(fill=X) 



def main(): 
    root = tk.Tk() 
    root.title("Menu") 
    WelcomeButton = tk.Label(root, text='Welcome to the menu!', width=50, height=2, fg="magenta") 
    WelcomeButton.pack(fill=X) 
    StartButton = tk.Button(root, text='Start treasure hunting!', width=50, fg="magenta", command = Treasure_Hunt_Window) 
    StartButton.pack(fill=X) 
    SettingsButton = tk.Button(root, text='''Change the settings of the treasure hunting game. 
This includes the grid size.''', width=50, fg="magenta", command = Settings_Window) 
    SettingsButton.pack(fill=X) 
    QuitButton = tk.Button(root, text='Exit the program', width=50, fg="magenta", command = root.destroy)# display message in a child window. 
    QuitButton.pack(fill=X) 
    root.mainloop() 

def teststuff(): 
    print(GridRows) 
    print(GridColumns) 

class GameBoard(tk.Frame): 
    def __init__(self, parent, size=48, color1="white", color2="black"): 
     '''size is the size of a square, in pixels''' 

     self.rows = GridRows 
     self.columns = GridColumns 
     self.size = size 
     self.color1 = color1 
     self.color2 = color2 
     self.pieces = {} 

     canvas_width = GridColumns * size 
     canvas_height = GridRows * size 

     tk.Frame.__init__(self, parent) 
     self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0, 
           width=canvas_width, height=canvas_height, background="green") 
     self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2) 

     self.canvas.bind("<Configure>", self.refresh) 


    def refresh(self, event): 
     '''Redraw the board, possibly in response to window being resized''' 
     xsize = int((event.width-1)/self.columns) 
     ysize = int((event.height-1)/self.rows) 
     self.size = min(xsize, ysize) 
     self.canvas.delete("square") 
     color = self.color2 
     for row in range(self.rows): 
      color = self.color1 if color == self.color2 else self.color2 
      for col in range(self.columns): 
       x1 = (col * self.size) 
       y1 = (row * self.size) 
       x2 = x1 + self.size 
       y2 = y1 + self.size 
       self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=color, tags="square") 
       color = self.color1 if color == self.color2 else self.color2 
     for name in self.pieces: 
      self.placepiece(name, self.pieces[name][0], self.pieces[name][1]) 
     self.canvas.tag_raise("piece") 
     self.canvas.tag_lower("square") 

main() 
+2

Immer die vollständige Rückverfolgung von Fehlern liefern! – tburrows13

+0

Diese Fehlermeldung kann möglicherweise nicht die eigentliche Fehlermeldung sein. Ich glaube nicht, dass irgendein Python-Fehler jemals mit etwas wie "wenn man Grid erstellt" endet. "Making" ist nicht Teil des Python-Exception-Vokabulars. –

+0

Bitte vandalisiere deine Beiträge nicht. Sobald Sie eine Frage gepostet haben, gehört sie zur gesamten Stack Overflow-Community (unter der CC-by-SA-Lizenz). Wenn Sie diesen Beitrag von Ihrem Konto trennen möchten, lesen Sie [Was ist der richtige Weg für eine Deaktivierungsanfrage?] (Https://meta.stackoverflow.com/questions/323395/what-is-the-proper-route- for-a-dissociation-request) – DSM

Antwort

0

Bitte weiter auf Ihrem Programm arbeiten, aber ersetzen Sie den Code mit dem folgenden und von dort aus arbeiten. Sie können es auch zu einer Übung machen, Ihren Code über die PEP8 online Website laufen zu lassen. Zu den an Ihrem Code vorgenommenen Änderungen gehört, dass keine Fehler oder Warnungen generiert wurden.

Um Ihre Frage zu beantworten, tun zwei der Funktionen unten etwas, das etwas von dem unterscheidet, was Ihr Code ursprünglich tat. Beachten Sie die ersten beiden Funktionen get_rows und get_columns. Wenn Sie Informationen von einer Spinbox erhalten, müssen Sie die Daten vor der Verwendung in den richtigen Typ konvertieren.

from tkinter import * 
import tkinter as tk 


GridSizeSetByUser = '8x8' 
choicesRows = ['8', '10', '12', '14'] 
v = choicesRows[0] 
choicesColumns = ['8', '10', '12', '14'] 
v2 = choicesColumns[0] 

GridRows = 9 
GridColumns = 9 

my_string = my_second_string = grid_row_spinbox = grid_column_spinbox = None 


def get_rows(): 
    global GridRows 
    GridRows = int(grid_row_spinbox.get()) 
    print(repr(GridRows)) 


def get_columns(): 
    global GridColumns 
    GridColumns = int(grid_column_spinbox.get()) 
    print(repr(GridColumns)) 


def treasure_hunt_window(): 
    t_hunt = tk.Tk() 
    t_hunt.title("Treasure Hunt") 
#  t_hunt_instructions = """\ 
# Find the treasure hidden deep in the sand!Use ye arrow keys to move around, 
# 
# then press Space to search that spot! Keep searching until ye find it!""" 
    board = GameBoard(t_hunt) 
    board.pack(side="top", fill="both", expand="true", padx=4, pady=4) 
    t_hunt.mainloop() 


def settings_window(): 
    global my_string, my_second_string, grid_row_spinbox, grid_column_spinbox 
    settings = tk.Tk() 
    settings.title("Settings") 
    settings_welcome = tk.Label(settings, text='Settings Menu', width=50, 
           fg="magenta") 
    settings_grid_size = tk.Label(settings, text='Grid Size:', width=50, 
            fg="magenta") 
    my_string = StringVar() 
    my_second_string = StringVar() 
    grid_row_spinbox = Spinbox(settings, values=choicesRows, 
           textvariable=my_string, width=50, 
           state="readonly", fg="magenta") 
    save_row_size = tk.Button(settings, text='Save row size for grid', 
           width=50, fg="magenta", command=get_rows) 
    grid_column_spinbox = Spinbox(settings, values=choicesColumns, 
            textvariable=my_second_string, 
            state="readonly", width=50, fg="magenta") 
    save_column_size = tk.Button(settings, text='Save column size for grid', 
           width=50, fg="magenta", command=get_columns) 
    # settings_bandits = tk.Label(settings, text='Amount of Bandits:', 
    #        width=50, fg="magenta") 
    bandit_amount = tk.Entry(settings, width=50, fg="magenta") 
    settings_bandits = tk.Label(settings, 
           text='Amount of Treasure Chests (up to 64)', 
           width=50, fg="magenta") 
    settings_welcome.pack(fill=X) 
    settings_grid_size.pack(fill=X) 
    grid_row_spinbox.pack(fill=X) 
    save_row_size.pack(fill=X) 
    grid_column_spinbox.pack(fill=X) 
    save_column_size.pack(fill=X) 
    settings_bandits.pack(fill=X) 
    bandit_amount.pack(fill=X) 


def main(): 
    root = tk.Tk() 
    root.title("Menu") 
    welcome_button = tk.Label(root, text='Welcome to the menu!', width=50, 
           height=2, fg="magenta") 
    welcome_button.pack(fill=X) 
    start_button = tk.Button(root, text='Start treasure hunting!', width=50, 
          fg="magenta", command=treasure_hunt_window) 
    start_button.pack(fill=X) 
    settings_button = tk.Button(root, text='''\ 
Change the settings of the treasure hunting game. 
This includes the grid size.''', width=50, fg="magenta", 
           command=settings_window) 
    settings_button.pack(fill=X) 
    # display message in a child window. 
    quit_button = tk.Button(root, text='Exit the program', width=50, 
          fg="magenta", command=root.destroy) 
    quit_button.pack(fill=X) 
    root.mainloop() 


def test_stuff(): 
    print(GridRows) 
    print(GridColumns) 


class GameBoard(tk.Frame): 
    def __init__(self, parent, size=48, color1="white", color2="black"): 
     """size is the size of a square, in pixels""" 

     self.rows = GridRows 
     self.columns = GridColumns 
     self.size = size 
     self.color1 = color1 
     self.color2 = color2 
     self.pieces = {} 

     canvas_width = GridColumns * size 
     canvas_height = GridRows * size 

     tk.Frame.__init__(self, parent) 
     self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0, 
           width=canvas_width, height=canvas_height, 
           background="green") 
     self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2) 

     self.canvas.bind("<Configure>", self.refresh) 

    def refresh(self, event): 
     """Redraw the board, possibly in response to window resize""" 
     x_size = int((event.width-1)/self.columns) 
     y_size = int((event.height-1)/self.rows) 
     self.size = min(x_size, y_size) 
     self.canvas.delete("square") 
     color = self.color2 
     for row in range(self.rows): 
      color = self.color1 if color == self.color2 else self.color2 
      for col in range(self.columns): 
       x1 = (col * self.size) 
       y1 = (row * self.size) 
       x2 = x1 + self.size 
       y2 = y1 + self.size 
       self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", 
              fill=color, tags="square") 
       color = self.color1 if color == self.color2 else self.color2 
     for name in self.pieces: 
      self.place_piece(name, self.pieces[name][0], self.pieces[name][1]) 
     self.canvas.tag_raise("piece") 
     self.canvas.tag_lower("square") 

    def place_piece(self, name, a, b): 
     pass 


if __name__ == '__main__': 
    main() 
+0

Sorry für die späte Antwort, aber danke eine Last dafür! – Timothy1224

+0

Wenn Sie die Antwort als hilfreich empfanden, sollten Sie die Empfehlungen auf dieser Seite berücksichtigen: [Was soll ich tun, wenn jemand meine Frage beantwortet?] (Http://stackoverflow.com/help/someone-answers) –

+0

Danke für deine Unterstützung! –

Verwandte Themen