kivy __init__ takes 1 postitional argument but 2 were given - python

So i'm learning Kivy for a school project and i got an error when testing out Buttons. Here is my Code:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.uix.button import Button
class übung(GridLayout):
def lol(instance):
label.disabled = False
def __init__(self):
super(übung, self).__init__
self.cols = 2
self.label = Label ("Ehrm ... lol")
label.disabled = True
self.btn1 = Button(text="Hello world 1")
self.btn1.bind(on_press=lol)
self.btn2 = Button(text="Hello world 2")
self.btn2.bind(on_press=lol)
class App(App):
def build(self):
return übung()
if __name__ == "__main__":
App().run()
The Error I'm getting is in the title(init takes 1 postitional argument but 2 were given). It is supposed to be two buttons and if you press one it say ehrm ... lol. As i said, it is just for testing purposes.
Thanks in Advance,
me

You have several errors. The error you display is because you have to pass the argument (text) to the Label constructor by name:
self.label = Label (text="Ehrm ... lol")
Your code should look something like this:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class übung(GridLayout):
def __init__(self, **kwargs):
super(übung, self).__init__(**kwargs)
self.cols = 2
self.label = Label(text = "Ehrm ... lol")
self.label.disabled = True
self.btn1 = Button(text="Hello world 1")
self.btn1.bind(on_press=self.lol)
self.btn2 = Button(text="Hello world 2")
self.btn2.bind(on_press=self.lol)
self.add_widget(self.label)
self.add_widget(self.btn1)
self.add_widget(self.btn2)
def lol(self, event):
self.label.disabled = False
class App(App):
def build(self):
return übung()
if __name__ == "__main__":
App().run()

Related

The Screen Manager is not returning Desired Output in Kivy Python

There is no such error shown in the Python output shell.
What I want is that , the first page should be the Login page or as here, the "ConnectingPage" then Welcome Should Be shown, and then at last a Set of buttons named from 0 to 99 be shown.
Here is the code :
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.core.window import Window
class ConnectingPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text = "Usename:"))
self.username = TextInput(multiline=False)
self.add_widget(self.username)
self.add_widget(Label(text = "Password:"))
self.password = TextInput(multiline=False,password = True)
self.add_widget(self.password)
self.joinbutton = Button(text="Join")
self.joinbutton.bind(on_release = self.click_join_button)
self.add_widget(Label())
self.add_widget(self.joinbutton)
def click_join_button(self, instance):
username = self.username.text
password = self.password.text
#if username == "venu gopal" and password == "venjar":
MyApp.screen_manager.current = "Info"
MyApp.screen_manager.current = "Chat"
class InfoPage(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
self.message = Label(text = "welcome",halign="center", valign="middle", font_size=30)
self.add_widget(self.message)
class SomeApp(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))
for i in range(100):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
root.add_widget(layout)
if __name__ == "__main__":
runTouchApp(root)
class MyApp(App):
screen_manager = ScreenManager(transition=FallOutTransition(duration=2)) # this make screen_manager a class vaiable
def build(self):
# self.screen_manager = ScreenManager()
self.connecting_page = ConnectingPage()
screen = Screen(name='Connect')
screen.add_widget(self.connecting_page)
self.screen_manager.add_widget(screen) # add screen to ScreenManager
# Info page
self.info_page = InfoPage()
screen = Screen(name='Info')
screen.add_widget(self.info_page)
self.screen_manager.add_widget(screen) # add screen to ScreenManager
# Chat App
self.chat_app = SomeApp()
screen = Screen(name='Chat')
screen.add_widget(self.chat_app)
self.screen_manager.add_widget(screen) # add screen to ScreenManager
# return ConnectingPage()
return self.screen_manager
if __name__ == "__main__":
MyApp().run()
The Problem is that: the set of Buttons are getting shown in the start. When the Cross is pressed to close the kivy window, then the Login page is shown and then "welcome" and then the buttons again.
I want It to show from the second Step.
What I believe is that the "line 61" in the code is making a problem. When the code is run it first shows the buttons and so on.
Please Help me find the solution for the above Problem.
Your __init__() method of the SomeApp class starts an App running with the lines:
if __name__ == "__main__":
runTouchApp(root)
and that runTouchApp() does not return until that App completes. So when you run your code, it stops a the line:
self.chat_app = SomeApp()
To fix that, just replace:
if __name__ == "__main__":
runTouchApp(root)
with:
self.add_widget(root)

Not able to print the text taken from kivy.uix.textinput.TextInput in KIVY Python

What I want to do is take input from kivy.uix.textinput.TextInput() and show it on the screen.
I am new to gui Programming and I think it is an easy task.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
class MyWindowApp(App):
def __init__(self):
super(MyWindowApp, self).__init__()
self.lbl = Label(text='Read Me!')
self.inp = TextInput(multiline=False,
size_hint =(1, 0.05),
pos_hint = {"x":0, "y":0.05})
def build(self):
self.inp.bind(on_text_validate=self.on_enter)
#self.bt1.bind(on_press=self.clk)
layout = FloatLayout()
layout.orientation = 'vertical'
layout.add_widget(self.lbl)
layout.add_widget(self.inp)
return layout
def on_enter(self,value):
print(value)
def clk(self, obj):
print ('input')
x = input()
self.lbl.text = x
window = MyWindowApp()
window.run()
when i run the code, I get the regular output output.
when I type say "hello world" in the textbox, this is the output:
<kivy.uix.textinput.TextInput object at 0x03F5AE30>
I do not get what I typed.
please suggest what should I do
Modify the following ...
def on_enter(self, value):
print(value.text)

Kivy [Freezes on button press]

so i'm trying to learn simple Kivy GUI to send SMS with a class i already made earlier, it works except the window freezes for a couple seconds, any tips on how to make the button press smooth and not freeze at all? Keep in mind the code below is just the beginning of the application, just to make sure it actually works. Thanks in advance.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.progressbar import ProgressBar
from kivy.config import Config
from sms_funksjon import SendSMS
Config.set('graphics', 'resizable', '0') # 0 being off 1 being on as in true/false
Config.set('graphics', 'width', '500')
Config.set('graphics', 'height', '200')
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
self.inside = GridLayout()
self.inside.cols = 2
self.inside.add_widget(Label(text="Mobil nummer: "), pow(50, 50))
self.mobile_number = TextInput(multiline=False)
self.inside.add_widget(self.mobile_number)
self.inside.add_widget(Label(text="Tekst: "))
self.tekst = TextInput(multiline=False)
self.inside.add_widget(self.tekst)
self.add_widget(self.inside)
self.pb = ProgressBar(max=100)
self.pb.value = 0
self.add_widget(self.pb)
self.submit = Button(text="Send", font_size=40)
self.submit.bind(on_press=self.pressed)
self.add_widget(self.submit)
def pressed(self, instance):
nummer = str(self.mobile_number.text)
tekst = str(self.tekst.text)
SendSMS(nummer, tekst)
self.clear_everything()
def clear_everything(self):
self.mobile_number.text = ""
self.tekst.text = ""
class Main(App):
def build(self):
return MyGrid()
Main().run()

Python: Kivy: Divide two number from a single TextInput

Simple question, how can i divide two numbers from a single InputBox? I have no idea, example, i have only one inputbox and i write two numbers "40 10" how can i divide automatically this? Here the code:
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListView
class Widget(GridLayout):
def __init__(self, **kwargs):
super(Widget, self) .__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text="RPM"))
self.rpm = TextInput(multiline=False)
self.add_widget(self.rpm)
btn1 = Button(text="Division:")
btn1.bind(on_press=self.buttonClicked)
self.add_widget(btn1)
Example of what i need:
def buttonClicked(self, btn):
self.rpm.text(first input / second input)
x = self.rpm.text
popup = Popup(title='Result', content=x, size_hint=(None,
None), size=(500, 90))
popup.open()
The procedure is the next:
Get the text from TextInput
Separate it by the space
Verify that only 2 terms exist
Convert it to float, so an error may appear, exceptions must be used.
And for the last one we establish the popup with a Label that contains the text.
import kivy
kivy.require("1.0.6")
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup
class Widget(GridLayout):
def __init__(self, **kwargs):
super(Widget, self) .__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text="RPM"))
self.rpm = TextInput(multiline=False)
self.add_widget(self.rpm)
btn1 = Button(text="Division:")
btn1.bind(on_press=self.buttonClicked)
self.add_widget(btn1)
def buttonClicked(self, btn):
texts = self.rpm.text.split()
if len(texts) == 2:
try:
x, y = map(float, texts)
res = x/y
popup = Popup(title='Result', content=Label(text=str(res)), size_hint=(None, None), size=(500, 90))
popup.open()
except (ValueError, ZeroDivisionError):
print("error")
class TestApp(App):
def build(self):
return Widget()
if __name__ == '__main__':
TestApp().run()
This type of interfaces are few useful since the user could place anything, the appropriate thing is to validate the text while it is being written as for example to accept a certain set of characters.
Another improvement is that it is separated into 2 TextInputs.

Create kivy dropdown list but only a part of them can be opened

I am using kivy as my GUI tools for my python program.
when I want to create a table,in which there is a column containing dropdown list to make select value easier.
However, I can not make it works correctly.
The following is my code.
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout
sel =["A","B","C"]
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.redraw()
def redraw(self):
self.clear_widgets()
self.rows = 5
self.cols =2
for i in range(5):
label = Label(text="cell"+str(i+1))
self.add_widget(label)
drpName = DropDown()
btnName = Button(text="B",size_hint=(None, None))
for e in sel:
btn=Button(text=e, size_hint_y=None, height=btnName.height)
btn.bind(on_release=lambda btn:drpName.select(btn.text))
drpName.add_widget(btn)
btnName.bind(on_release=drpName.open)
drpName.bind(on_select=lambda instance, x: setattr(btnName, 'text', x))
self.add_widget(btnName)
class testApp(App):
def build(self):
return MyGrid()
if __name__=="__main__":
testApp().run()
Only a part of button open the pull down list and all of the selected value will replace the text of last button.
Could you help me out. Thanks in advance.
After reading post Python Lambda in a loop[Building Dropdowns Dynamically in Kivy, I can make my program works.
Thanks for valuable post.
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout
sel =["A","B","C"]
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.redraw()
def redraw(self):
self.clear_widgets()
self.rows = 5
self.cols =2
drpName = []
for i in range(5):
label = Label(text="cell"+str(i+1))
self.add_widget(label)
drpName.append(DropDown())
btnName=Button(text="B",size_hint=(None, None))
for e in sel:
btn=Button(text=e, size_hint_y=None, height=btnName.height)
btn.bind(on_release=lambda btn=btn,dropdown=drpName[i]:dropdown.select(btn.text))
drpName[i].add_widget(btn)
btnName.bind(on_release=drpName[i].open)
drpName[i].bind(on_select=lambda instance, x,btn=btnName: setattr(btn, 'text', x))
self.add_widget(btnName)
class testApp(App):
def build(self):
return MyGrid()
if __name__=="__main__":
testApp().run()

Categories