- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在程序中不断遇到此错误,并且我不知道如何修复它,因为我是 python 3 的初学者。每当我尝试更改菜单中网格的大小时,就会发生该错误。然后我去运行宝藏网格,我得到这个错误并且不知道如何解决。感谢任何和所有的帮助!
错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\python\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "/Documents/treasurehunt test.py", line 178, in refresh
placeChests()
File "/Documents/treasurehunt test.py", line 128, in placeChests
while len(treasureChests) < treasure_amount:
TypeError: unorderable types: int() < Entry()
代码:
from tkinter import *
import tkinter as tk
import random
GridSizeSetByUser = '8x8'
choicesRows = ['8', '10', '12', '14']
v = choicesRows[0]
choicesColumns = ['8', '10', '12', '14']
v2 = choicesColumns[0]
treasure_amount = 10
treasureChests = []
treasureLocation = []
GridRows = 10
GridColumns = 10
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 create_window():
t_child = tk.Toplevel()
t_child.title("Instructions")
tInstructions = tk.Label(t_child, fg="magenta",text="""Find the treasure hidden deep in the sand!\n Use ye arrow keys to move around, then press Space to search that spot!\n Keep searching until ye find it!""")
tInstructions.pack(side="top", fill="both", expand=False, padx=10, pady=10)
quit_button = tk.Button(t_child, text="I'm ready to find some treasure!", width=50, fg="magenta", command=t_child.destroy)
quit_button.pack()
t_child.attributes('-topmost', True) # note - before topmost
def treasure_hunt_window():
t_hunt = tk.Tk()
t_hunt.title("Treasure Hunt")
board = GameBoard(t_hunt)
board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
create_window()
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")
global treasure_amount
treasure_amount = tk.Entry(settings, width=50, fg="magenta")
settings_Treasure = 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_Treasure.pack(fill=X)
treasure_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)
def key_pressed(self, event):
if event.keysym == "Right" and self.current_pos[1] < 7:
self.current_pos[1] += 1
elif event.keysym == "Left" and self.current_pos[1] > 0:
self.current_pos[1] -= 1
elif event.keysym == "Up" and self.current_pos[0] > 0:
self.current_pos[0] -= 1
elif event.keysym == "Down" and self.current_pos[0] < 7:
self.current_pos[0] += 1
elif event.keysym == "Return":
self.process_guess()
self.matrix = [["#" for col in range(8)] for row in range(8)]
def placeChests():
while len(treasureChests) < treasure_amount:
x = random.randrange(GridColumns)
y = random.randrange(GridRows)
treasureLocation = [x, y]
if treasureLocation not in treasureChests:
global treasureChests
treasureChests.append(treasureLocation)
print (treasureChests)
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 addpiece(self, name, image, row=0, column=0):
'''Add a piece to the playing board'''
self.canvas.create_image(0,0, image=image, tags=(name, "piece"), anchor="c")
self.placepiece(name, row, column)
def placepiece(self, name, row, column):
'''Place a piece at the given row/column'''
self.pieces[name] = (row, column)
x0 = (column * self.size) + int(self.size/2)
y0 = (row * self.size) + int(self.size/2)
self.canvas.coords(name, x0, y0)
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
placeChests()
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")
if __name__ == '__main__':
main()
最佳答案
仅处理该特定问题,您正在比较 int()
与 tk.Entry()
IE。 len(treasureChests) < treasure_amount
len 返回 int
和treasure_amount
是输入框。要解决该问题,请将 while 循环更改为:
while len(treasureChests) < int(treasure_amount.get()):
使用.get()
它检索输入框的值和 int()
从默认 str
转换稍后可能会出现更多问题。
作为旁注,请尝试少用 global
声明。
编辑进一步检查后,我发现这是一个常见的名称错误,您将两个不同的对象设置为同一变量。第一个位于顶部:
treasure_amount = 10
另一个位于您的设置窗口中:
global treasure_amount
treasure_amount = tk.Entry(settings, width=50, fg="magenta")
所以你应该更改其中一个的名称。
关于python - 不可排序的类型 : int() < Entry(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42853345/
Eclipse 提示此代码“类型参数 Entry 隐藏了类型 Map.Entry”: import java.util.Map.Entry; public class Test { stat
这两个for语句是等价的。 import java.util.Map; import java.util.Map.Entry; for (Map.Entry entry: map.entrySet()
我想在 python 字典中添加一个键/值,它依赖于现有的键/值。示例 x = {} x["length"] = 12 x["volume"] =x["lenght"] * 10 行得通;但是有没有可
我使用 NuGet 将我的 EntityModel 升级到版本 4.3。 现在我想更改我的 EntityObject.State,但找不到 .Entry() 方法。 当前状态是已删除。 这就是我想要做
我是 Java 新手,正在使用 HashMap 在 Mac 上编写 Java。 但是我遇到了一个问题,找不到答案 import java.util.Map; import java.util.Hash
我正在切换一个小应用程序(Win 7/64 上的 Python 2.7.3/32)来使用 ttk,但我在使 ttk.Entry 按照 tk.Entry 的方式工作时遇到问题;当我设置其内容时,ttk.
为什么我需要在 i.next(); 前面加上 (Map.Entry) ?为什么我不能只有 Map.Entry m = i.next();? 对不起。它是一个 HashMap。 最佳答案 因为它显然不是
我的 xPage SSJS 失败: viewEntry = view.getNext(viewEntry); 有错误 Notes error: Entry not found in index 我确实
我正在使用 DataTable在我的申请中。 我想隐藏左下角的细节,我该怎么做? “显示 1,657 个条目中的 1 到 10 个(从 9,044 个条目中筛选出来)” 这是我的设置: $('#inv
我有两列,一个时间戳和一个已选择卡片的数字。 card added 1 2016-09-23 13:48:48 3 2016-09-23 13:48:48 1
我正在使用 Entity Framework 4.1,并且我有我的 DbContext Override SaveChanges 来审核属性更改。从“GetEntryValueInString”返回空
我正在使用 Tkinter 在 Python 3 上编写 GUI,但每次使用 Entry() 时,我都会收到名称错误。 我尝试了一个更简单的代码版本,(写在下面),但它仍然导致了 NameError:
我刚刚创建了一个插入方法来对数组进行排序,这是我在该方法中完成的代码; public static void insertionSort (Entry[] array2){ for (int
使用这个例子1对于 n:n 关系: (来源:tekstenuitleg.net) 设置主要或主要多对多字段的最佳方法是什么?示例:假设我想将经销商“Devrolijke drinker”(ID AB9
是否可以使用 Entry 通过 AsRef 获取值的 API , 但用 Into 插入它? 这是工作示例: use std::collections::hash_map::{Entry, HashMa
是否可以使用 Entry 通过 AsRef 获取值的 API , 但用 Into 插入它? 这是工作示例: use std::collections::hash_map::{Entry, HashMa
是否可以使用 Entry 通过 AsRef 获取值的 API , 但用 Into 插入它? 这是工作示例: use std::collections::hash_map::{Entry, HashMa
表定义: CREATE TABLE PositionalDataNGS ( Date DATE, Time TIME(3) , X FLOAT(5), Y FLOAT(5), D FLOAT(5) ,
我的平台: PHP 和 MySQL 我这里有什么: 我有 4 个表,分别是“books”、“book_type”、“book_categories”、“all_categories”。 我想做什么:
I am using MySQL 5.1.56, MyISAM. My table looks like this:我使用的是MySQL 5.1.56,MyISAM。我的桌子是这样的: CR
我是一名优秀的程序员,十分优秀!