I have already tried the location to the font thing, like this:
Label:
font_name: 'Titr.ttf'
font_size: 34
text: "پاسمار"
i used b titr, i renamed b titr to titr, it does find it as the ttf is in the same folder, i can see the differences, when there isn't font_name: 'Titr.ttf', it gives out a bunch of random things, but when the titr is there, its only vertical rectangles.
#! /usr/local/bin/python -*- coding: UTF-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelHeader
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from functools import partial
from kivy.base import runTouchApp
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
import sys
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.lang import Builder
from kivy.base import runTouchApp
import random
import time
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty, NumericProperty
import time
import threading
class ScreensApp(App):
def build(self):
return RootWidget()
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
def __init__(self, **kwargs):
super(ScreenTwo, self).__init__(**kwargs)
class RootWidget(FloatLayout):
pass
class ImageButton(ButtonBehavior, Image):
def on_press(self):
print ('pressed')
def displayScreenThenLeave(self):
self.changeScreen()
def changeScreen(self):
if self.manager.current == 'screen1':
self.manager.current = 'screen2'
else:
self.manager.current = 'screen1'
class Manager(ScreenManager):
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=NoTransition())
return m
if __name__ == "__main__":
ScreensApp().run()
class The_AssignmentApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
The_AssignmentApp().run()
#:kivy 1.10.1
-- coding: utf-8 --
<ScreenOne>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 15
padding: 200
Label:
font_name: 'Titr.ttf'
font_size: 34
text: "پاسمار"
Button:
background_color: 0, 255, 0, .255
text: "Start Pasmar"
on_release: root.manager.current = "screen2"
<ScreenTwo>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
ImageButton:
Image:
source:'Card_club.png'
size_hint: .2, .2
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
ScreenOne:
id: screen_one
name: "screen1"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "screen2"
manager: screen_manager
Related
since print('hello') is working after pressing enter in Text Input but Why don't these two work?
self.grid2.add_widget(Button())
self.grid2.add_widget(TextInput())
Code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
Builder.load_string("""
<Button>
size_hint: (None, None)
height: 33
width: 100
text:'1'
background_normal: ''
background_down:'1, .3, .4, .85'
background_color: 1, .3, .4, .85
<TextInput>
selection_color:1, .3, .4, .4
on_text_validate:app.enter()
cursor_color: 255/255, 223/255, 5/255, 1
multiline:False
height: 33
width:800
size_hint: (None, None)
background_color: 0,0,0,0
foreground_color: 255/255, 143/255, 5/255, 0.8
<Grid>
t1:t1
b1:b1
grid:grid
grid2:grid2
GridLayout:
cols:1
id:grid
size: root.width - 400,root.height - 400
pos:100,100
GridLayout:
id:grid2
cols:2
Button:
id:b1
TextInput:
id:t1
""")
class Grid(Widget):
t1=ObjectProperty(None)
b1=ObjectProperty(None)
grid=ObjectProperty(None)
grid2=ObjectProperty(None)
def enter(self):
print('hello')
self.grid2.add_widget(Button())
self.grid2.add_widget(TextInput())
gr=Grid()
class foo(App):
def build(self):
Window.clearcolor='#1618388'
return Grid()
def enter(self):
gr.enter()
if __name__ == '__main__':
foo().run()
it worked for me if i returned gr instead for Grid()
class foo(App):
def build(self):
Window.clearcolor = '#1618388'
return gr
def enter(self):
gr.enter()
I would like to set player speed, but i get this error AttributeError: 'super' object has no attribute '__getattr__. How can I work with that speed in other class. I have no idea, why is there this error, because I found self.ids on internet. Those people didn't have that error, but I have,why? How to solve it? Thanks
My python:
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import RoundedRectangle, Color
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.floatlayout import FloatLayout
from kivy.lang.builder import Builder
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.config import Config
from kivy.uix.label import Label
from kivy.properties import NumericProperty
Config.set('graphics', 'fullscreen', 'auto')
Config.set('graphics', 'window_state', 'maximized')
Builder.load_file("game.kv")
class Menu(Screen):
pass
class LevelSelection(Screen):
pass
class Level1(Screen):
def on_enter(self, *args):
Clock.schedule_interval(self.update, 1/30)
def update(self, *args):
self.ids.player.speed += 500* 1/30
self.ids.Player.y += self.ids.player.speed * 1 /30
class Game(Screen):
pass
class Player(Image):
speed = NumericProperty(0)
class Full(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Menu(name="menu"))
sm.add_widget(LevelSelection(name="levels"))
sm.add_widget(Game(name="game"))
sm.add_widget(Level1(name="level1"))
return sm
if __name__ == "__main__":
Full().run()
My .kv:
<MenuButton#Button>:
font_size :"50sp"
background_normal : "bt1_normal.png"
background_down : "bt1_down.png"
border:90,90,90,90
<Level#Button>:
font_size :"50sp"
background_normal : "bt2_normal.png"
background_down : "bt2_down.png"
<LevelSelection>:
BoxLayout:
canvas:
Color:
rgba:1,1,1,1
Rectangle:
pos:self.pos
size:self.size
source:"backgrounf.png"
orientation:"vertical"
padding:"100dp", "10dp"
spacing:"150dp"
Widget:
BoxLayout:
padding:"100dp"
spacing:"100dp"
Level:
text:"1"
on_release:
app.root.current = "level1"
<Menu>:
BoxLayout:
canvas:
Color:
rgba:1,1,1,1
Rectangle:
pos:self.pos
size:self.size
source:"backgrounf.png"
orientation:"vertical"
padding:"200dp", "50dp"
spacing:"150dp"
Widget:
BoxLayout:
padding:"100dp"
spacing:"150dp"
MenuButton:
text:"start"
on_release:
app.root.current = "levels"
MenuButton:
text:"exit"
on_release:
app.stop()
<Player>:
source:"player.png"
<Level1>:
FloatLayout
Player:
id:player
allow_stretch:True
size_hint:None,None
size:root.height/15,root.height/15
x:root.width/20
y:root.height/2
I'm trying to add a changeable list in a custom dialog in kivyMD. the problem is when I call self.adding() , the list appears in my FloatLayout, not in the dialog.
I know that's because I called "Content" in my FloatLayout. I did this because I wanted to use ObjectProperty but it didn't work well. I really don't know what can I do to solve this problem.
Sorry for my broken English.
code:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem,MDList
from kivy.properties import ObjectProperty
KV = '''
<Content>
name:"content"
draw:container
orientation: "vertical"
ScrollView:
MDList:
id: container
FloatLayout:
mgr:cont
Content:
id:cont
MDFlatButton:
text: "ALERT DIALOG"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_confirmation_dialog()
'''
class Content(BoxLayout):
draw=ObjectProperty(None)
class Example(MDApp):
dialog = None
mgr=ObjectProperty(None)
def adding(self):
for i in range(20):
self.root.mgr.draw.add_widget(OneLineListItem(text=f"Single-line item {i}"))
def build(self):
return Builder.load_string(KV)
def show_confirmation_dialog(self):
# def adding(self):
# for i in range(20):
# container.add_widget(OneLineListItem(text=f"Single-line item {i}"))
if not self.dialog:
self.dialog = MDDialog(
title="Address:",
type="custom",
content_cls=Content(),
)
self.adding()
self.dialog.open()
Example().run()
I fixed the problem by editing my code to this :
NEW CODE:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem,MDList
KV = '''
<Content>
# name:"content"
# draw:container
orientation: "vertical"
ScrollView:
MDList:
id: container
FloatLayout:
# mgr:cont
# Content:
# id:cont
MDFlatButton:
text: "ALERT DIALOG"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_confirmation_dialog()
'''
class Content(BoxLayout):
def __init__(self, *args, **kwargs):
super().__init__(**kwargs)
self.container= self.ids.container
print("content called")
def adding(self):
for i in range(20):
self.container.add_widget(OneLineListItem(text=f"Single-line item {i}"))
print("adding called")
adding(self)
class Example(MDApp):
dialog = None
def build(self):
return Builder.load_string(KV)
def show_confirmation_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title="Address:",
type="custom",
content_cls=Content(),
)
self.dialog.open()
Example().run()
i have an app, that in the future, will be a cards game counter, but right now its nowhere near that stage, anyways, i have 3 files, main.py, screens.kv,screens2.kv(which doesnt have anything in it) now i also got a picture, and its a button, each time its pressed, for test it writes down "pressed", i've been on this for at least 2 hours and cant figure out how to make the button appear when i press "Start Pasmar" (Pasmar is the name of the game in future), all it brings up is a blank screen after i press the button, but i did replace them and it only showed the button, nothing else.
This my current code:
#-*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelHeader
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from functools import partial
from kivy.base import runTouchApp
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
import sys
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.lang import Builder
from kivy.base import runTouchApp
import random
import time
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty, NumericProperty
import time
import threading
class ScreensApp(App):
def build(self):
return RootWidget()
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
def __init__(self, **kwargs):
super(ScreenTwo, self).__init__(**kwargs)
class RootWidget(FloatLayout):
pass
class ImageButton(ButtonBehavior, Image):
def on_press(self):
print ('pressed')
def displayScreenThenLeave(self):
self.changeScreen()
def changeScreen(self):
if self.manager.current == 'screen1':
self.manager.current = 'screen2'
else:
self.manager.current = 'screen1'
class Manager(ScreenManager):
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=NoTransition())
return m
if __name__ == "__main__":
ScreensApp().run()
class The_AssignmentApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
The_AssignmentApp().run()
This is the kv file:
#:kivy 1.10.1
<ScreenOne>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 15
padding: 200
Label:
font_size: 34
text: "Pasmar"
Button:
background_color: 0, 255, 0, .255
text: "Start Pasmar"
on_release: root.manager.current = "screen2"
<ScreenTwo>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
ScreenOne:
id: screen_one
name: "screen1"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "screen2"
manager: screen_manager
I am trying to make a screen view with buttons. The problem is that the amount of buttons needed each time will change, therefore I am not able to use the kv file to make theses buttons. I am having trouble adding buttons through the normal python code. Any help is appreciated.
import kivy
import webbrowser
import MSQLHandler
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.uix.listview import ListItemButton
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition, CardTransition, SwapTransition, FadeTransition, WipeTransition, FallOutTransition, RiseInTransition, NoTransition
from kivy.lang import Builder
from kivy.uix.button import Button
class Login_Failed(Screen):
def changeScreen(self, next_screen):
self.manager.current = next_screen
class Loged_In(Screen):
def changeScreen(self, next_screen):
self.manager.current = next_screen
class Registers(Screen):
def changeScreen(self, next_screen):
self.manager.current = next_screen
class Login_Screen(Screen):
teacher_connect_image = ObjectProperty()
username_text_input = ObjectProperty()
password_text_input = ObjectProperty()
teacherid_text_input = ObjectProperty()
def LoginButton(self):
if self.teacherid_text_input.text == "":
Number = 0
else:
Number = int(self.teacherid_text_input.text)
MSQLHandler.LoginSystem(self.username_text_input.text, self.password_text_input.text, Number)
def changeScreen(self, next_screen):
self.manager.current = next_screen
if MSQLHandler.LoginSystem(self.username_text_input.text, self.password_text_input.text, Number) is True:
print("Returns True")
changeScreen(self, "Loged_In")
elif MSQLHandler.LoginSystem(self.username_text_input.text, self.password_text_input.text, Number) is False:
print("Returns False")
changeScreen(self, "Login_Failed")
else:
print("Null")
class ScreenManagerr(ScreenManager):
pass
class MainKivyFrameApp(App):
def build(self):
Registers().add_widget(Button(text="Helpp"))
return ScreenManagerr()
mainFrame = MainKivyFrameApp()
mainFrame.run()
If you look to where the app is being built, you will see: Registers().add_widget(Button(text="Helpp"))
This is where I have tried to add a Button to the screen of Registers. This doesn't give me any errors, but it still does not show the button.
Solution
In the kv file, add an event (on_enter or on_pre_enter) in each screen and bind it to a callback method as shown in the following snippets and example. Remember to remove the widgets that were added dynamically before leaving the current screen, by adding an event (on_leave or on_pre_leave).
Snippets
<Registers>:
on_pre_enter: self.add_buttons(n)
on_leave: self.remove_buttons()
Example
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.properties import ObjectProperty, NumericProperty
class MyScreenManager(ScreenManager):
total_button = NumericProperty(2)
class Login(Screen):
login = ObjectProperty(None)
def on_pre_enter(self):
Window.size = (400, 300)
def check_password(self, instance, password):
if password == "pwd":
instance.current = "registers"
class Registers(Screen):
container = ObjectProperty(None)
def on_pre_enter(self):
Window.size = (800, 600)
def add_buttons(self, n):
print("Registers: n={}".format(n))
for i in range(n):
self.container.add_widget(Button(text="Button #{}".format(i), id=str(i)))
def remove_buttons(self, *args):
for child in [child for child in self.container.children]:
self.container.remove_widget(child)
class Welcome(Screen):
pass
class TestApp(App):
title = "ScreenManager - Add Widgets Dynamically"
def build(self):
return MyScreenManager()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.10.0
#:import SwapTransition kivy.uix.screenmanager.SwapTransition
<MyScreenManager>:
transition: SwapTransition()
Login:
Registers:
on_pre_enter:
self.add_buttons(app.root.total_button)
on_leave:
self.remove_buttons()
Welcome:
<Login>:
id: login
name: "login"
login: login
GridLayout:
size_hint: (0.5, 0.5)
pos_hint: {"center_x": 0.5, "center_y": 0.6}
rows: 3
padding: 20
Label:
size_hint: (0.2, 0.2)
text:"Password:"
font_size: 30
halign: "center"
valign: "middle"
TextInput:
id: password
size_hint: (0.2, 0.06)
cursor_blink: True
font_size: 20
multiline: False
password: True
Button:
text: "Continue"
size_hint: (0.2, 0.08)
on_release:
root.login.check_password(root.manager, password.text)
<Registers>:
name:'registers'
container: container
BoxLayout:
orientation: 'vertical'
Button:
text: 'Return to Login'
on_press: root.manager.current = 'login'
Button:
text: 'Next Screen'
on_press: root.manager.current = 'welcome'
BoxLayout:
id: container
orientation: 'vertical'
<Welcome>:
name:'welcome'
BoxLayout:
Label:
text: 'Welcome!'
Button:
text: 'Return to Registers'
on_press:
app.root.total_button = 5
root.manager.current = 'registers'
Output