When I was trying to use grid layout, it shows errors when I try to use more widgets but with one widget it shows no errors,I have reviewed the codes many times but i didn't find any solutions,and by the way I'm using python 3.7 and kivy version 2 Here's my code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class My_Grid(GridLayout):
def __init__(self, **kwargs):
super(My_Grid, self).__init__(**kwargs)
self.cols = 4
self.rows = 3
self.add_widget(Label(text="First Name: "))
self.FirstName = TextInput(multiline=False)
self.add_widget(self.FirstName)
self.add_widget(Label(text="Last Name: "))
self.LastName = TextInput(mulyiline=False)
self.add_widget(self.LastName)
self.add_widget(Label(text="email: "))
self.Email = TextInput(mulyiline=False)
self.add_widget(self.Email)
class TestApp(App):
def build(self):
return My_Grid()
if __name__ == "__main__":
TestApp().run()
the errors when i use more widgets:
Traceback (most recent call last):
File "C:/Users/Asus/PycharmProjects/untitled3/my.py", line 29, in <module>
TestApp().run()
File "C:\Users\Asus\PycharmProjects\untitled3\venv\lib\site-packages\kivy\app.py", line 829, in run
root = self.build()
File "C:/Users/Asus/PycharmProjects/untitled3/my.py", line 25, in build
return My_Grid()
File "C:/Users/Asus/PycharmProjects/untitled3/my.py", line 18, in __init__
self.LastName = TextInput(mulyiline=False)
File "C:\Users\Asus\PycharmProjects\untitled3\venv\lib\site-packages\kivy\uix\textinput.py", line 527, in __init__
super(TextInput, self).__init__(**kwargs)
File "C:\Users\Asus\PycharmProjects\untitled3\venv\lib\site-packages\kivy\uix\behaviors\focus.py", line 367, in __init__
super(FocusBehavior, self).__init__(**kwargs)
File "C:\Users\Asus\PycharmProjects\untitled3\venv\lib\site-packages\kivy\uix\widget.py", line 350, in __init__
super(Widget, self).__init__(**kwargs)
File "kivy\_event.pyx", line 243, in kivy._event.EventDispatcher.__init__
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
Process finished with exit code 1
At first, you have a mistake in the word 'multiline' in self.LastName and self.Email. Try to fix that.
Related
I am implementing a GUI project, and I want to use multiprocessing to accelerate the calculation. In order to tell the user that the calculation is finished, I use a threading to check whether the calculation is finished. My code is:
import sys
import threading
from PyQt5.QtWidgets import *
import multiprocessing
import time
class SubProcess(multiprocessing.Process):
def __init__(self, calFunc):
super().__init__()
self.calFunc = calFunc
self.recv, self.send = multiprocessing.Pipe(False)
def run(self) -> None:
self.calFunc()
self.send.send(True)
def IsFinished(self):
return self.recv.recv()
class SubThread(threading.Thread):
def __init__(self, calFunc, finishFunc):
super().__init__()
self.finishFunc = finishFunc
self.subProcess = SubProcess(calFunc)
def start(self) -> None:
super().start()
self.subProcess.start()
def run(self) -> None:
while self.subProcess.IsFinished() != True:
pass
self.finishFunc()
class Widget(QWidget):
def __init__(self):
super().__init__()
self.btn = QPushButton('calculation')
self.btn.clicked.connect(self.calBtnClick)
self.label = QLabel('not finish')
self.lay = QVBoxLayout()
self.setLayout(self.lay)
self.lay.addWidget(self.btn)
self.lay.addWidget(self.label)
def calBtnClick(self, check=False):
print('calculation')
self.subThread = SubThread(self.calculation, self.finish)
self.subThread.start()
def finish(self):
print('calculation finish')
self.label.setText('finish')
def calculation(self):
time.sleep(5)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Widget()
win.show()
app.exec()
When press the QPushButton, I want Widget.calculation run in the sub-process, and a sub-threading will check whether the sub-process is finished.
However, the code reports a bug:
Traceback (most recent call last):
File "C:/Users/zhq/Desktop/test/test2.py", line 58, in calBtnClick
self.subThread.start()
File "C:/Users/zhq/Desktop/test/test2.py", line 32, in start
self.subProcess.start()
File "C:\ProgramData\Anaconda3\envs\MedPro\lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
File "C:\ProgramData\Anaconda3\envs\MedPro\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\ProgramData\Anaconda3\envs\MedPro\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\ProgramData\Anaconda3\envs\MedPro\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
reduction.dump(process_obj, to_child)
File "C:\ProgramData\Anaconda3\envs\MedPro\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle Widget objects
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\ProgramData\Anaconda3\envs\MedPro\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\ProgramData\Anaconda3\envs\MedPro\lib\multiprocessing\spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
The bug is reported for self.subProcess.start().
How to fix this bug? Any suggestion is appreciated~~~
Update
Thank #Dipet, the code is modifed as:
import sys
import threading
from PyQt5.QtWidgets import *
import multiprocessing
import time
def calFunc():
time.sleep(5)
class SubProcess(multiprocessing.Process):
def __init__(self, calFunc):
super().__init__()
self.calFunc = calFunc
self.recv, self.send = multiprocessing.Pipe(False)
def run(self) -> None:
self.calFunc()
self.send.send(True)
def IsFinished(self):
return self.recv.recv()
class SubThread(threading.Thread):
def __init__(self, calFunc, finishFunc):
super().__init__()
self.finishFunc = finishFunc
self.subProcess = SubProcess(calFunc)
def start(self) -> None:
super().start()
self.subProcess.start()
def run(self) -> None:
while self.subProcess.IsFinished() != True:
pass
self.finishFunc()
class Widget(QWidget):
def __init__(self):
super().__init__()
self.btn = QPushButton('calculation')
self.btn.clicked.connect(self.calBtnClick)
self.label = QLabel('not finish')
self.lay = QVBoxLayout()
self.setLayout(self.lay)
self.lay.addWidget(self.btn)
self.lay.addWidget(self.label)
def calBtnClick(self, check=False):
print('calculation')
self.subThread = SubThread(calFunc, self.finish)
self.subThread.start()
def finish(self):
print('calculation finish')
self.label.setText('finish')
def calculation(self):
time.sleep(5)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Widget()
win.show()
app.exec()
Multiprocessing module provide args using pickle, so all values that you provided into subproces must support pickle protocol. You use self.finish as an argument for your worker, but this is a Widget method, but QWidget do not support pickling. And you see and error: TypeError: can't pickle Widget objects. Try to use staticmethod or common function.
I want to update my label in python after the loop has started.
Sadly I get the error
File "mygui.py", line 19, in <module>
GUI.user_said("General Kenobi")
File "mygui.py", line 16, in user_said
self.my_label['text'] = self.label_text
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1486, in __setitem__
self.configure({key: value})
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1479, in configure
return self._configure('configure', cnf, kw)
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1470, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label"
which doesn't help me at all...
My Code:
from tkinter import *
class MyGUI:
"""A simple class"""
def __init__(self):
self.__main_window = Tk()
self.label_text = "Hello There"
self.my_label = Label(self.__main_window, text=self.label_text)
self.my_label.pack()
self.__main_window.mainloop()
def user_said(self, users_request):
"""Returns what the user says"""
self.label_text = "You said:\n{}\n\n".format(users_request)
self.my_label.config(text=self.label_text)
GUI = MyGUI()
GUI.user_said("General Kenobi")
I'm very glad if someone helps me to find a solution for my problem.
Because MyGUI.__init__ calls self.__main_window.mainloop(), it will not return until the main window is destroyed. Therefore, by the time you call GUI.user_said("General Kenobi"), none of the widgets exist any more.
I am trying to implement a diagram tool in Kivy, but I'm seeing the below error when I try to instantiate an instance of the FlowChartNode class:
Traceback (most recent call last):
File "FlowChartExample.py", line 193, in <module>
FlowChartExampleApp().run()
File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 798, in run
root = self.build()
File "FlowChartExample.py", line 189, in build
root = FlowChartExampleWidget()
File "FlowChartExample.py", line 184, in __init__
begin_node = FlowChartNode()
File "FlowChartExample.py", line 116, in __init__
super(FlowChartNode, self).__init__(**kwargs)
File "/usr/lib/python2.7/dist-packages/kivy/uix/behaviors.py", line 105, in __init__
super(ButtonBehavior, self).__init__(**kwargs)
File "/usr/lib/python2.7/dist-packages/kivy/uix/label.py", line 187, in __init__
super(Label, self).__init__(**kwargs)
File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 261, in __init__
super(Widget, self).__init__(**kwargs)
File "_event.pyx", line 252, in kivy._event.EventDispatcher.__init__ (kivy/_event.c:4505)
File "_event.pyx", line 777, in kivy._event.EventDispatcher.properties (kivy/_event.c:7967)
TypeError: 'ObservableList' object is not callable
Here's the FlowChartNode class:
class FlowChartNode(Button):
#Exposes the event on_properties to allow for binding to expose
#properties panel in app on triple-press
properties = ListProperty([0, 0])
#Properties to set the backgrounds of the node
background_draggable = StringProperty('img/drag_node_small.png')
background_pressable = StringProperty('img/press_node_small.png')
background_dragged = StringProperty('img/drag_node_down_small.png')
background_pressed = StringProperty('img/press_node_down_small.png')
#The components of the node
front = ObjectProperty(None)
back = ListProperty([])
connect = ListProperty([])
#The sparse grid being added to
sparse_grid = ObjectProperty(None)
#The assigned cell in the sparse grid
cell = ObjectProperty(None)
#Boolean state for draggable or pressable
is_draggable = BooleanProperty(False)
def __init__(self, **kwargs):
super(FlowChartNode, self).__init__(**kwargs)
if self.is_draggable:
self.background_normal = self.background_draggable
else:
self.background_normal = self.background_pressable
self.bind(pos=self.set_front)
def on_touch_down(self, touch):
if touch.is_triple_tap:
self.properties = touch.pos
elif touch.is_double_tap:
if self.is_draggable:
self.is_draggable=False
self.background_normal = self.background_pressable
else:
self.is_draggable=True
self.background_normal = self.background_draggable
else:
if self.is_draggable:
self.background_normal = self.background_pressed
touch.grab(self)
else:
self.background_normal = self.background_dragged
#Add the connected node
back_node = FlowChartNode(is_draggable=True)
connector = FlowConnector()
back_node.bind(pos=self.set_back)
self.back.append(back_node)
self.connect.append(connector)
self.connect[len(connect) - 1].front = self.front.center
self.cell.add_widget(connector)
self.cell.add_widget(back_node)
return super(FlowChartNode, self).on_touch_down(touch, *args)
def on_touch_move(self, touch):
if touch.grab_current == self:
if self.collide_point(touch.pos):
for cel in self.sparse_grid.cells:
if cel.collide_point(touch.pos):
#Snap the grabbed node to the cell
self.cell.clear_widgets()
self.cell = cel
cel.add_widget(self)
return super(FlowChartNode, self).on_touch_move(touch, *args)
def on_touch_up(self, touch):
return super(FlowChartNode, self).on_touch_up(touch, *args)
if self.is_draggable:
self.background_normal = self.background_draggable
else:
self.background_normal = self.background_pressable
def set_front(self, *args):
for con in self.connect:
con.front = self.center_node.center
def set_back(self, *args):
i=0
for con in self.connect:
con.back = self.options[i].center
i+=1
Most of the code came from a working widget which can be found here, as well as other successful tests and working widgets. I've reviewed all of the Kivy Properties I'm interacting with and they appear to be handled correctly. What am I doing wrong here?
Thanks in advance for your help!
Alex
I ended up re-writing the classes a bit differently, you can find the full code here if you are interested.
I am getting this error when run horizon in devstack in eclipse. may be it's configuration error but I cannot solve it. please help
WARNING:root:No local_settings file found.
Traceback (most recent call last):
File "/home/stack/git/horizon/manage.py", line 23, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8-py2.7.egg/django/core/management/__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "/usr/local/lib/python2.7/dist-packages/Django-1.8-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8-py2.7.egg/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8-py2.7.egg/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/stack/git/horizon/openstack_dashboard/settings.py", line 339, in <module>
from horizon.utils import secret_key
File "/home/stack/git/horizon/horizon/__init__.py", line 27, in <module>
from horizon.base import Dashboard # noqa
File "/home/stack/git/horizon/horizon/base.py", line 45, in <module>
from horizon import loaders
File "/home/stack/git/horizon/horizon/loaders.py", line 57, in <module>
_loader = TemplateLoader()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8-py2.7.egg/django/template/loader.py", line 50, in __init__
super(BaseLoader, self).__init__(*args, **kwargs)
TypeError: __init__() takes exactly 2 arguments (1 given)
Django loader file is showing error when run horizon. it showed type error in argument
import warnings
from django.utils.deprecation import RemovedInDjango20Warning
from .base import Origin
from .engine import Engine
class LoaderOrigin(Origin):
def __init__(self, display_name, loader, name, dirs):
super(LoaderOrigin, self).__init__(display_name)
self.loader, self.loadname, self.dirs = loader, name, dirs
def reload(self):
return self.loader(self.loadname, self.dirs)[0]
def find_template(*args, **kwargs):
return Engine.get_default().find_template(*args, **kwargs)
def get_template(*args, **kwargs):
return Engine.get_default().get_template(*args, **kwargs)
def get_template_from_string(*args, **kwargs):
return Engine.get_default().get_template_from_string(*args, **kwargs)
def render_to_string(*args, **kwargs):
return Engine.get_default().render_to_string(*args, **kwargs)
def select_template(*args, **kwargs):
return Engine.get_default().select_template(*args, **kwargs)
# This line must remain at the bottom to avoid import loops.
from .loaders import base
class BaseLoader(base.Loader):
_accepts_engine_in_init = False
def __init__(self, *args, **kwargs):
warnings.warn(
"django.template.loader.BaseLoader was superseded by "
"django.template.loaders.base.Loader.",
RemovedInDjango20Warning, stacklevel=2)
super(BaseLoader, self).__init__(*args, **kwargs)
Horizon requires Django>=1.4.2,<1.7, you're using Django 1.8 here (see requirements.txt)
The __init__ arguments for BaseLoader have changed between those two versions.
You need to fix your environment and dependencies if you want this to work.
May be there is no. of criteria for that type of problem ,
but in my case i am not initialize all the parameters in __init__() method
so this create a problem
def __init__(self, value, *args, **kwargs):
self.value = value
super(Custom_Vali, self).__init__(*args, **kwargs)
....
....
Run
test_c = Custom_Vali.get_error(89)
OR
test_c = Custom_Vali.get_error(value = 89)
O/P: TypeError: __init__() takes at least 2 arguments (1 given)
SOLUTION:
just set value=None or value = 0 in __init__
def __init__(self, value=0, *args, **kwargs):
self.value = value
super(Custom_Vali, self).__init__(*args, **kwargs)
First the code:
main.py
import string
from app import App
group1=[ "spc", "bspc",",","."]#letters, space, backspace(spans mult layers)
# add in letters one at a time
for s in string.ascii_lowercase:
group1.append(s)
group2=[0,1,2,3,4,5,6,7,8,9, "tab ","ent","lAR" ,"rAR" , "uAR", "dAR"]
group3= []
for s in string.punctuation:
group3.append(s)#punc(spans mult layers)
group4=["copy","cut","paste","save","print","cmdW","quit","alf","sWDW"] #kb shortcut
masterGroup=[group1,group2,group3,group4]
myApp =App({"testFKey":[3,2,2,None})
app.py
import tkinter as tk
import static_keys
import dynamic_keys
import key_labels
class App(tk.Frame):
def __init__(inputDict,self, master=None):
tk.Frame.__init__(self, master)
self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
self.createWidgets(self, inputDict)
def createWidgets(self,inDict):
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
tempDict = {}
for k,v in inDict.items():
if 1<=v[0]<=3:
tempDict[k] = static_keys(k,*v[1:])
elif v[0] ==4:
tempDict[k] = dynamic_keys(k,*v[1:])
elif v[0]==5:
tempDict[k] = key_labels(k,*v[1:])
for o in tempDict:
tempDict[o].grid()
return tempDict
static_keys.py
import tkinter
class StaticKeys(tkinter.Label):
"""class for all keys that just are initiated then do nothing
there are 3 options
1= modifier (shift etc)
2 = layer
3 = fkey, eject/esc"""
def __init__(t,selector,r,c, parent,self):
if selector == 1:
tkinter.Label.__init__(master=parent, row=r, column=c, text= t, bg ='#676731')
if selector == 2:
tkinter.Label.__init__(master=parent, row=r, column=c, text= t, bg ='#1A6837')
if selector == 3:
tkinter.Label.__init__(master=parent, row=r, column=c, text= t, bg ='#6B6966')
When I run main.py I get:
Traceback (most recent call last):
File "Desktop/kblMaker/main.py", line 13, in <module>
myApp =App({"testFKey":[3,2,2]})
File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 8, in __init__
tk.Frame.__init__(self, master)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2574, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2067, in __init__
self.widgetName = widgetName
AttributeError: 'dict' object has no attribute 'widgetName'
Why is this? I have a hunch that I need to possibly do more attribute = self.attribute stuff in the inits, but I don't know. That construct confuses me. Also, is the style good? It doesn't seem like i seem many people tasking all their gui construction into one function. If it is poor, please suggest alternatives. Thanks so much for your help!
update1
I followed the advice of #zhangyangyu by changing the order of arguments and got:
Traceback (most recent call last):
File "Desktop/kblMaker/main.py", line 14, in <module>
myApp =App(d)
File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 10, in __init__
self.createWidgets(self, inputDict)
TypeError: createWidgets() takes 2 positional arguments but 3 were given
update2
Traceback (most recent call last):
File "Desktop/kblMaker/main.py", line 14, in <module>
myApp =App(d)
File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 10, in __init__
self.createWidgets(inputDict)
File "/Users/fozbstudios/Desktop/kblMaker/app.py", line 20, in createWidgets
tempDict[k] = StaticKeys(k,*v[1:])
TypeError: __init__() missing 2 required positional arguments: 'parent' and 'self'
update3
now got it down to missing only one arg by adding a parent string in d, changed main.py to reflect. Seems like its wanting me too pass a self, even hough I know i shouldn't
def __init__(inputDict,self, master=None):
This part of your code is wrong. In your code, self is the argument you pass in and inputDict will be the instance of the class. You'd better use:
def __init__(self, inputDict, master=None):