Im writing a Sudoku solver UI in Kivy.
I wanted to add a button, which upon being pressed, solves the Grid. The function works, although when I assign it to a button as the on_press function, it somehow runs before the button is pressed. I've tried everything. I get an Assertion Error. When I just use the solve function, it works. Not with the button, though.
Thank you for helping.
This is the __init__ function:
class SudokuGrid(GridLayout):
def __init__(self, **kwargs):
super(SudokuGrid, self).__init__(**kwargs)
self.grid = [[8,7,0, 0,0,0, 0,0,0],
[0,0,0, 9,0,0, 0,0,4],
[0,2,0, 7,0,0, 1,0,5],
[0,0,9, 6,0,0, 0,3,0],
[0,0,0, 0,0,0, 0,0,9],
[0,0,6, 5,4,0, 0,0,0],
[6,9,0, 0,0,0, 7,0,0],
[2,0,0, 0,0,7, 4,0,0],
[0,0,0, 3,0,0, 0,1,0]]
self.add_widget(Button(on_press=self.solve()))
self.cols = 3
self.rows = 4
self.load_grid()
And this my solve function:
def solve(self):
for x in range(9):
for y in range(9):
if self.grid[y][x] == 0:
for n in range(1,10):
if self.possible(y,x,n):
self.grid[y][x] = n
self.solve()
self.grid[y][x] = 0
return
self.load_grid()
print(np.matrix(self.grid))
I know that the function is run, because before the App terminates, I get the output of the solved Grid.
This is the Traceback:
Traceback (most recent call last):
File "C:\Users\victo\Desktop\portfolio\Sudoku\sudoku_app.py", line 217, in <module>
sudoku_game.run()
File "C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\app.py", line 829, in run
root = self.build()
File "C:\Users\victo\Desktop\portfolio\Sudoku\sudoku_app.py", line 205, in build
self.sudoku_grid = SudokuGrid()
File "C:\Users\victo\Desktop\portfolio\Sudoku\sudoku_app.py", line 73, in __init__
self.add_widget(Button(on_press=self.solve()))
File "C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\behaviors\button.py", line 121, in __init__
super(ButtonBehavior, self).__init__(**kwargs)
File "C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\label.py", line 318, in __init__
super(Label, self).__init__(**kwargs)
File "C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\widget.py", line 369, in __init__
self.bind(**on_args)
File "kivy\_event.pyx", line 419, in kivy._event.EventDispatcher.bind
AssertionError: None is not callable
Okay so i figured it out. Erik said in his comment:
Try on_press=self.solve in stead of on_press=self.solve()
which worked, although I got another Error. I had one too many positional arguments for solve(). I fixed it by defining my method as:
def solve(self, *args)
Everything works now.
Related
I hope it run and do not stop.
I'm making Conway's Game of Life,
if my code look stupid, please help me, I'm only 11.
I use the details from wiki, if it's wrong , please tell me.
Thankyou!
import turtle,random
START_POSITION=[]
def ReStart():
global START_POSITION
#START_POSITION.clear()
y=500
x=-500
for i in range(1,26):
for a in range(1,26):
START_POSITION.append(eval(f"({x},{y})"))
x+=20
x=(0-300)
y-=20
return True
ReStart()
screen=turtle.Screen()
class Cell:
def __init__(self):
self.cells=[]
self.make_body()
self.a()
self.Alive(screen)
def make_body(self):
global START_POSITION
for i in START_POSITION:
seg=turtle.Turtle(shape="square")
seg.color("White")
seg.penup()
seg.goto(i[0],i[1])
self.cells.append(seg)
The error saids:
Traceback (most recent call last):
File "C:/Users/****/Desktop/寫程式/the life game.py", line 145, in <module>
cell=Cell()
File "C:/Users/****/Desktop/寫程式/the life game.py", line 20, in __init__
self.make_body()
File "C:/Users/****/Desktop/寫程式/the life game.py", line 29, in make_body
seg.goto(i[0],i[1])
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1777, in goto
self._goto(Vec2D(x, y))
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 3180, in _goto
self._update()
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 2661, in _update
self._update_data()
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 2647, in _update_data
self.screen._incrementudc()
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1293, in _incrementudc
raise Terminator
turtle.Terminator
I'm totally stuck on this,please help me.
I've reworked your code fragment into what I believe you are trying to do. Avoid eval as it can cause endless problems. Your use of global in this context isn't valid. See if this works for you:
from turtle import Screen, Turtle
class Body:
def __init__(self, positions):
self.cells = []
self.make_body(positions)
# self.a()
# self.Alive(screen)
def make_body(self, positions):
for position in positions:
cell = Turtle(shape='square')
cell.fillcolor('white')
cell.penup()
cell.goto(position)
self.cells.append(cell)
def restart():
start_positions.clear()
y = 250
for _ in range(25):
x = -250
for _ in range(25):
start_positions.append((x, y))
x += 20
y -= 20
start_positions = []
screen = Screen()
screen.setup(550, 550)
screen.bgcolor('black')
screen.tracer(False)
restart()
my_body = Body(start_positions)
screen.tracer(True)
screen.exitonclick()
Since I've turned off tracer() for speed, call screen.update() whenever you're ready for the user to see the most recent changes.
I'm really new to python and I don't even know if the title of this python question is even right. Anyways I'm facing an error code that I have been trying to find a fix for in the last couple of hours. The error goes like this: raise RuntimeError("threads can only be started once").
Now I googled the error and I got a couple of solutions. I tried a couple of them and none of them seem to work. I know that you cannot use the start function for more than one time, But how else am I supposed to get my script to work again?
import pyautogui, requests, pyperclip
from pynput import mouse, keyboard
from tkinter import filedialog
url = "https://poxgur.com/"
file_path = filedialog.asksaveasfilename(defaultextension='.png')
def on_click(x, y, button, pressed):
global currentMouseX, currentMouseY
if pressed:
currentMouseX, currentMouseY = x, y
if not pressed:
mouse_listener.stop()
im = pyautogui.screenshot(region=(
currentMouseX, currentMouseY, abs(currentMouseX - x), abs(currentMouseY - y)))
im.save(file_path)
print(file_path)
files = {"file": open(file_path, "rb")}
r = requests.post(url + "api.php", files=files)
pyperclip.copy(url + r.text + ".png")
# os.remove(file_path)
mouse_listener.stop()
return False
mouse_listener = mouse.Listener(on_click=on_click)
def on_scroll_lock_release(key):
if key == keyboard.Key.scroll_lock:
if not mouse_listener.is_alive():
mouse_listener.start()
with keyboard.Listener(on_release=on_scroll_lock_release) as listener:
listener.join()
The error meesage:
Unhandled exception in listener callback
Traceback (most recent call last):
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 162, in inner
return f(self, *args, **kwargs)
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\keyboard\_win32.py", line 283, in _process
self.on_release(key)
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 78, in inner
if f(*args) is False:
File "C:/Users/marti/Documents/PythonProjects/Acutal Projects/The 100 Projects/Screenshot2.0.py", line 33, in on_scroll_lock_release
mouse_listener.start()
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\threading.py", line 848, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
Traceback (most recent call last):
File "C:/Users/marti/Documents/PythonProjects/Acutal Projects/The 100 Projects/Screenshot2.0.py", line 37, in <module>
listener.join()
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 210, in join
six.reraise(exc_type, exc_value, exc_traceback)
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 162, in inner
return f(self, *args, **kwargs)
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\keyboard\_win32.py", line 283, in _process
self.on_release(key)
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 78, in inner
if f(*args) is False:
File "C:/Users/marti/Documents/PythonProjects/Acutal Projects/The 100 Projects/Screenshot2.0.py", line 33, in on_scroll_lock_release
mouse_listener.start()
File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\threading.py", line 848, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
Process finished with exit code 1
A possible solution would be to intialize a new Listener each time before starting the Listener.
You could re-code your solution in the following way:
mouse_listener = mouse.Listener(on_click=on_click)
def on_scroll_lock_release(key):
if key == keyboard.Key.scroll_lock:
if not mouse_listener.is_alive():
mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()
I want to create a custom ListCtrl "MyListCtrlCrafting" which pulls the data from a different class called "DBInterface" (actually it is not a real database, but a sophisticated python dictionary). There are tutorials on how to do that, I followed "Python in Action". The main points are: inherit from ListCtrl an set the style parameter to wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_VIRTUAL, call SetItemCount() during init() and override some methods.
To test how that works I made a small App which consists only of one (main-)frame, the virtual ListCtrl and a rudimentary version of DBInterface. Everything works out fine in this setting. But when I connect the classes of the real App, I get a Traceback:
Traceback (most recent call last):
File "DesktopCrafter.py", line 198, in <module>
controller = AppCtrl()
File "DesktopCrafter.py", line 186, in __init__
self.frame = GUI.MainWindow(back_end=self.back_end, crafting_controller=self.crafting_controller, parent=None, title='Desktop Crafter')
...
self.view = MyListCtrlCrafting(name='crafting-hand', back_end=self.db, crafting_controller=self.cc, parent=self, id=wx.ID_ANY)
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 107, in __init__
self.bindData()
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 121, in bindData
self.SetItemCount(count)
wx._core.wxAssertionError: C++ assertion "m_count == ListView_GetItemCount(GetHwnd())" failed at ..\..\src\msw\listctrl.cpp(3120) in wxListCtrl::SetItemCount(): m_count should match ListView_GetItemCount
In contrast to the simple App, the virtualListCtrl is now deeply nested. Can this error just be produced by wrong connections inside this nesting or between DBInterface and the ListCtrl? Or do I have to understand how m_count is calculated, to solve this error? If so, how can I read the _core file? I already read about ListCtrl in the core.py file, but it doesn't contain the relevant parts.
My problem with this traceback is that I don't understand why it is raised during SetItemCount(). This method should be something like a definition and since it is dealing with rows of a list, it should accept positive integers, and maybe 0, and maybe also -1 for standard. I plug in 5, so this cannot be the real problem going on here(?)
Any help or hint is much appreciated!
The complete Traceback:
Traceback (most recent call last):
File "DesktopCrafter.py", line 198, in <module>
controller = AppCtrl()
File "DesktopCrafter.py", line 186, in __init__
self.frame = GUI.MainWindow(back_end=self.back_end, crafting_controller=self.crafting_controller, parent=None, title='Desktop Crafter')
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 285, in __init__
self.InitUI()
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 299, in InitUI
self.panel = MainPanel(back_end=self.db, crafting_controller=self.cc)
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 251, in __init__
self.splitter = MySplitter(back_end=back_end, crafting_controller=crafting_controller)
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 229, in __init__
self.l_frame = LPanel(back_end=back_end, crafting_controller=crafting_controller)
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 188, in __init__
self.panel = CraftingPanel(back_end=back_end, crafting_controller=crafting_controller, *args, **kwargs)
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 154, in __init__
self.view = MyListCtrlCrafting(name='crafting-hand', back_end=self.db, crafting_controller=self.cc, parent=self, id=wx.ID_ANY)
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 107, in __init__
self.bindData()
File "d:\workspace-fun\DesktopCrafter\dc\util\DCUI.py", line 121, in bindData
self.SetItemCount(count)
wx._core.wxAssertionError: C++ assertion "m_count == ListView_GetItemCount(GetHwnd())" failed at ..\..\src\msw\listctrl.cpp(3120) in wxListCtrl::SetItemCount(): m_count should match ListView_GetItemCount
The virtual ListCtrl (both print's give me the expected results):
class MyListCtrlCrafting(wx.ListCtrl):
def __init__(self, name, back_end, crafting_controller, *args, **kwargs):
wx.ListCtrl.__init__(self, style=wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_VIRTUAL, *args, **kwargs)
self.name = name
self.db = back_end
self.cc = crafting_controller
#print(self.db.retrieveValue(['player', self.name]))
self.Bind(wx.EVT_LIST_CACHE_HINT, self.DoCacheItems)
self.bindData()
self.InsertColumn(0, "Slot")
self.InsertColumn(1, "Item")
self.InsertColumn(2, "Amount")
print("initialized MyListCtrl")
def bindData(self):
data = self.db.retrieveValue(['player', self.name])
count = len(data)
#print(count)
self.itemDataMap = {i: ('slot'+str(i+1), data[str(i)]['item'], data[str(i)]['amount']) for i in range(count)}
self.SetItemCount(count)
def DoCacheItems(self, e):
self.db.updateCache(e.GetCacheFrom(), e.GetCacheTo())
def OnGetItemText(self, row_no, col_no):
data = self.db.retrieveValue(['player', self.name, str(row_no)])
return data[col_no]
def OnGetItemAttr(self, item): return None
def OnGetItemImage(self, item): return -1
def OnBackEndUpdated(self):
self.bindData()
self.RefreshItems(0, self.GetItemCount())
The DBInterface:
class DBInterface(dict):
def __init__(self, path, *args, **kwargs):
super(DBInterface, self).__init__(*args, **kwargs)
self.path = path
def load(self):
with open(self.path, 'r') as f:
try:
self.update(json.loads(f.read()))
except Exception as e:
print(e); print(self.path)
def save(self):
self.path.ensure()
with open(self.path, 'w') as f:
f.write(json.dumps(self))
def retrieveValue(self, path):
a = self
for i in range(len(path)):
a = a[path[i]]
return a
That assert is basically a check to ensure that the operation (setting the count) was successful, by asking the native control how many items it has (which should have been set by the prior API call. It seems like something that really shouldn't be able to fail, although obviously it is. Perhaps something is resetting or changing the native control somehow? Are there any threads involved?
I don't have a solid answer for you, but as advice I would suggest taking the small sample that works and build it up step by step until it matches the window hierarchy and environment and basic features of that part of the full application where it is broken. Eventually it should start failing the same way and then you'll know what changed that triggered it and can look more closely at that.
Whenever I try to run my code, which looks a bit like this:
from tkinter import OptionMenu, StringVar, Tk
class Foo(OptionMenu):
def __init__(self, master, options):
self.bar = StringVar()
self.bar.set(options[0])
self.bar.trace("w", lambda: self.mouseCallback("foobar"))
super().__init__(master, self.bar, *options)
def mouseCallback(self, baz):
print(baz)
def mainCycle():
while True:
root.update()
if __name__ == "__main__":
opts = ["A", "LONG", "LIST", "OF", "OPTIONS"]
root = Tk()
foobarbaz = Foo(root, opts)
foobarbaz.pack()
mainCycle()
I get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 3285, in __call__
self.__var.set(self.__value)
File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 260, in set
return self._tk.globalsetvar(self._name, value)
_tkinter.TclError: can't set "PY_VAR0":
And even after a lot of "Stack Overflowing", I still can't get it to work. How can I avoid/fix this error?
The signature of the callback function for StringVar.trace() should be something like def callback(*args), therefore the lambda you use in StringVar.trace() should be changed to:
self.bar.trace("w", lambda *args: self.mouseCallback("foobar"))
My program gives me this error:
Traceback (most recent call last):
File "C:\Simulation\SymDialog.py", line 153, in OnPaint
self.Redraw(False)
File "C:\Simulation\SymDialog.py", line 173, in Redraw
obj.Draw(self.dc)
File "C:\Simulation\SymDialog.py", line 207, in Draw
dc.DrawCircle(self._x, self._y, self._r)
File "E:\Python\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 3391, in DrawCircle
return _gdi_.DC_DrawCircle(*args, **kwargs)
OverflowError: Python int too large to convert to C long
Method from error:
OnPaint
def OnPaint(self, event):
self.Redraw(False)
wx.GCDC(wx.BufferedPaintDC(self, self._buffer))
event.Skip()
Redraw
def Redraw(self, clear):
if clear == True: del self.drawList[:]
self.dc = wx.GCDC(wx.BufferedDC(None, self._buffer))
self.dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
self.dc.Clear()
for obj in self.drawList:
obj.Draw(self.dc)
del self.dc
Draw
def Draw(self, dc):
self.setDC(dc)
dc.DrawCircle(self._x, self._y, self._r)
How can I fix this error ?
Thanks for answers
You might be mistakenly using too large values.
Edit: Since I can't comment, what are the sizes of self._x, self._y, and self._r?