how to use GridLayout in TabeedPanel using kivy in python - python

I am trying to make a GUI in python using kivy and TabeedPanel . some problems are coming for putting on exact location of label, TextInput , button. I'm unable to put multiple label, TextInput altogether. That's why I commented in the code. I tried GridLayout also, but Unable to arrange exactly.
Can you help me? Thanks in advance.
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.lang import Builder
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.textinput import TextInput
import json
Builder.load_string("""
<Test>:
do_default_tab: False
TabbedPanelItem:
text: 'page1'
BoxLayout:
Label:
text: 'label'
TextInput:
text: 'TextInput'
CheckBox:
text: 'CheckBox'
Button:
text: 'save'
#BoxLayout:
# orientation: 'vertical'
# BoxLayout:
# orientation: 'horizontal'
# Label:
# text: 'label'
TabbedPanelItem:
text: 'page2'
BoxLayout:
Label:
text: 'number1'
#TextInput:
# text: 'TextInput'
Label:
text: 'number2'
# TextInput:
# text: 'TextInput'
Button:
text: 'button'
""")
class Test(TabbedPanel):
pass
class MyApp(App):
def build(self):
test = Test()
return test
if __name__ == '__main__':
MyApp().run()

Following your example, you can use BoxLayouts but you need to nest them correctly:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
Builder.load_string("""
<Test>:
do_default_tab: False
TabbedPanelItem:
text: 'page1'
BoxLayout:
padding: 50, 50, 50, 50
orientation: 'horizontal'
BoxLayout:
spacing: 50
orientation: 'vertical'
size_hint_x: 1
Label:
text: 'label'
Label:
text: 'label'
Label:
text: 'label'
BoxLayout:
spacing: 50
orientation: 'vertical'
TextInput:
text: 'TextInput'
TextInput:
text: 'TextInput'
TextInput:
text: 'TextInput'
BoxLayout:
spacing: 50
orientation: 'vertical'
size_hint_x: 0.40
CheckBox:
text: 'CheckBox'
CheckBox:
text: 'CheckBox'
CheckBox:
text: 'CheckBox'
BoxLayout:
spacing: 50
orientation: 'vertical'
size_hint_x: 0.60
Button:
text: 'save'
Button:
text: 'save'
Button:
text: 'save'
TabbedPanelItem:
text: 'page2'
BoxLayout:
padding: 50, 50, 50, 50
orientation: 'horizontal'
BoxLayout:
spacing: 50
orientation: 'vertical'
Label:
text: 'label'
Label:
text: 'label'
Label:
BoxLayout:
spacing: 50
orientation: 'vertical'
TextInput:
text: 'TextInput'
TextInput:
text: 'TextInput'
Button:
spacing: 100
text: 'button'
""")
class Test(TabbedPanel):
pass
class MyApp(App):
def build(self):
test = Test()
return test
if __name__ == '__main__':
MyApp().run()
Output:

Here's an example using GridLayout that I made a reference to in your other question. FYI, there are many ways you could go about this. I personally like using gridlayout with forms because it's easy to put then ScrollViews if need be.
Read up on the kv language here to help keep things DRY and other things. If a widget is defined in kv, then you don't need to import them at the top of your file.
Example:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
Builder.load_string("""
<MyLabel#Label>:
size_hint: (None, None)
size: (400, 100)
<MyTextInput#TextInput>:
size_hint: (None, None)
size: (600, 100)
<MyButton#Button>:
size_hint: (None, None)
size: (400, 100)
<MyCheckBox#AnchorLayout>:
# I'm nesting the checkbox here b/c it is hard to see if the background is not lightened.
size_hint: (None, None)
size: (100, 100)
anchor_x: "center"
anchor_y: "center"
canvas.before:
Color:
rgba: [0.7, 0.7, 0.7, 1]
Rectangle:
pos: self.pos
size: self.size
CheckBox:
<Test>:
do_default_tab: False
TabbedPanelItem:
text: 'page1'
GridLayout:
rows: 3
cols: 4
padding: [10, 100]
spacing: [10, 50]
MyLabel:
text: "Label 1"
MyTextInput:
MyCheckBox:
MyButton:
text: "Button 1"
MyLabel:
text: "Label 3"
MyTextInput:
MyCheckBox:
MyButton:
text: "Button 2"
MyLabel:
text: "Label 3"
MyTextInput:
MyCheckBox:
MyButton:
text: "Button 3"
TabbedPanelItem:
text: 'page2'
GridLayout:
rows: 3
cols: 2
padding: [10, 100]
spacing: [10, 50]
MyLabel:
text: "Label 1"
MyTextInput:
MyLabel:
text: "Label 2"
MyTextInput:
# blank spacer widget
Widget:
size_hint: (None, None)
size: (400, 100)
MyButton:
text: "Button"
""")
class Test(TabbedPanel):
pass
class MyApp(App):
def build(self):
return Test()
if __name__ == '__main__':
MyApp().run()

Related

Delete a row in Kivy's BoxLayout

With Kivy, I want a layout that is different when the window size is portrait or landscape (a grid of buttons, and for exemple the buttons on a row are shown in a column).
I tried first to hide some widgets, but that doesn't work well (even at a size of 0, some text appears, and this doesn't work with spacing)
So I wanted to delete a row directly, it worked, but...
The space for the raw remains with, like this
I tried to delete the second row, but there is now a gap.
Here is the code for that example
test3.kv file
<MDBoxLayout>:
spacing:5
<MyLayout>:
MDBoxLayout:
orientation: "vertical"
size: root.width, root.height
id: vbox
# Texte
MDLabel:
size_hint: 1,0.4
text: "Text"
halign: 'left'
valign: 'bottom'
font_size: 48
id: label
# Ligne 1
MDBoxLayout:
orientation: "horizontal"
size_hint: 1,0.15
id: ligne1
MDFillRoundFlatButton:
size_hint: 0.25,1
text: "A"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"B"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"C"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"D"
# Ligne 2
MDBoxLayout:
orientation: "horizontal"
size_hint: 1,0.15
id: ligne2
MDFillRoundFlatButton:
size_hint: 0.25,1
text: "1"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"2"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"3"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"4"
# Ligne 3
MDBoxLayout:
orientation: "horizontal"
size_hint: 1,0.15
MDFillRoundFlatButton:
size_hint: 0.25,1
text: "E"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"F"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"G"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"H"
# Ligne 4
MDBoxLayout:
orientation: "horizontal"
size_hint: 1,0.15
MDFillRoundFlatButton:
size_hint: 0.25,1
text: "I"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"J"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"K"
MDFillRoundFlatButton:
size_hint: 0.25,1
text:"L"
test3.py file
import kivy
kivy.require('1.1.1')
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivymd.uix.boxlayout import MDBoxLayout
Builder.load_file('test3.kv')
class MyLayout(Widget):
def on_size(self, *args):
self.ids.label.text = 'on_size'
if self.width > self.height:
self.ids.label.text = self.ids.label.text + f' horizontal {self.ids.ligne2}'
# orientation : horizontal
if isinstance(self.ids.ligne2, MDBoxLayout):
self.ids.label.text = self.ids.label.text + ' ligne 2 existe: supprimée'
self.ids.ligne2.clear_widgets()
self.remove_widget(self.ids.ligne2)
self.ids.ligne2 = None
class app(MDApp):
def build(self):
return MyLayout()
if __name__ == '__main__':
app().run()
Is this something missing in my code, or should I try another method to change dynamically the layout?

Getting a black screen when using screenmananger in python/kivy

I'm trying to build an app using kivy and python. When I implement screen manager I get a black screen though. As you can see in my code I have two screens: ApppScreen and AllitemsScreen. I'd like to have my ApppScreen to come up first as 'homescreen'. What is going wrong? Thanks in advance!
kv file:
<ApppScreen>:
Label:
text: 'hoi'
BoxLayout:
size: root.width, root.height
orientation: 'vertical'
Label:
text: 'hey'
size_hint: 1, .1
ScrollView:
size_hint: 1, .3
GridLayout:
size_hint: 1,None
height: self.minimum_height
cols: 1
MyLabel:
text: ' items'
GridLayout:
id: container
cols: 2
size_hint_y: None
height: self.minimum_height
row_force_default: True
row_default_height: dp(260)
TextInput:
title: 'NewItem'
id: input
multiline: False
size_hint: 1, .1
Button:
text: 'add new item'
size_hint: 1, .1
on_press: app.add_a_row()
Button:
text: 'All items'
size_hint: 1, .1
on_press: root.manager.current = 'allitems'
<AllitemsScreen>:
BoxLayout:
size: root.width, root.height
orientation: 'vertical'
Label:
text: 'hey'
size_hint: 1, .1
ScrollView:
size_hint: 1, .3
GridLayout:
size_hint: 1,None
height: self.minimum_height
cols: 1
MyLabel:
text: 'All items'
GridLayout:
size_hint: 1,None
height: self.minimum_height
cols: 2
rows: 25
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
MyLabel:
text: 'Filler'
TextInput:
title: 'NewItem'
id: input
multiline: False
size_hint: 1, .1
Button:
text: 'add new item'
size_hint: 1, .1
on_press: root.newitem(input.text)
Button:
text: 'items2'
size_hint: 1, .1
on_press: root.manager.current = 'appp'
<TextInp>:
title: 'NewItem'
id: test1
auto_dismiss: False
TextInput:
multiline: False
id: input
hint_text:'Enter the items url'
on_text: app.NewItem()
<Row>:
orientation: "horizontal"
Image:
source: app.image_source
width: 100
main.py file
import kivy
kivy.require('1.9.0')
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix import image
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from price import ExtractPrice
from pythonmain import Item
from image import ExtractImage
import requests
from urllib.request import urlopen
from PIL import Image
from datetime import date
class ApppScreen(Screen):
def newitem(self, link):
item_list.append(Item(link))
print(item_list[0].store, item_list[0].original_price)
class AllitemsScreen(Screen):
pass
sm = ScreenManager()
sm.add_widget(ApppScreen(name='appp'))
sm.add_widget(AllitemsScreen(name='allitems'))
class Row(BoxLayout):
pass
class notifier(App):
image_source = item1.image
print(image_source)
def build(self):
return sm
def add_a_row(self):
self.root.ids.container.add_widget(Row())
if __name__ == '__main__':
notifier().run()
You create your widgets before loading the kv. Create them in the build method instead.

Passing a text input between screens in Kivy

I am having trouble taking a text input value from one screen and passing it as the text in a label in another screen. I want to take the text input from a TeamNameSelect screen and have those be the text values in the labels of a GameWindow screen. I've tried going through similar questions and answers on here but have been unable to get this to work. Any help would be greatly appreciated!
.py file
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.widget import Widget
class NewGame(Screen):
pass
class GameWindow(Screen):
def teamNames(self, *args):
self.teamOne_input.text = self.manager.ids.TeamNameSelect.ids.teamOne.text
self.teamTwo_input.text = self.manager.ids.TeamNameSelect.ids.teamTwo.text
pass
class TeamNameSelect(Screen):
pass
class WinMan(ScreenManager):
pass
kv = Builder.load_file("my.kv")
sm = WinMan()
screens = [NewGame(name='goBack'), TeamNameSelect(name='teamSelect'), GameWindow(name='startGame')]
for screen in screens:
sm.add_widget(screen)
sm.current = 'goBack'
class MyApp(App):
def build(self):
return sm
if __name__ == '__main__':
MyApp().run()
.kv file
<TeamNameSelect>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
padding: 10
Label:
text: 'Team 1 Name: '
TextInput:
id: teamOne
text: ''
multiline: False
BoxLayout:
orientation: 'vertical'
padding: 10
Label:
text: 'Team 2 Name: '
TextInput:
id: teamTwo
text: ''
multiline: False
BoxLayout:
Button:
text: 'Go Back'
on_release: root.manager.current = 'goBack'
Button:
text: 'Game On!'
on_release:
root.manager.current = 'gameWindow'
root.teamNames()
<GameWindow>:
teamOne_input: teamOne_input
teamTwo_input: teamTwo_input
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: (1, 0.1)
Button:
text: '. . .'
on_release: root.manager.current = 'goBack'
Label:
font_size: 33
text: 'Team'
Label:
font_size: 33
id: teamOne_input
text: ''
Label:
font_size: 33
text: 'Team'
Label:
font_size: 33
id: teamTwo_input
text: ''
BoxLayout:
orientation: 'horizontal'
size_hint: (0.75,1)
Label:
font_size: 33
text: '' # Instructions on how to play game
Label:
font_size: 39
text: '' # Future playing area to develop
You have 2 preliminary errors:
There is no Screen with name "gameWindow" so I suppose the OP wanted to write "startGame".
The root in on_release is the "TeamNameSelect" that clearly has nothing that does not have the teamNames() method.
On the other hand the "manager" is not implemented in the .kv so it cannot have any "id", the solution is to access the screen with name "teamSelect" using the get_screen() method.
Considering the above, the solution is:
class GameWindow(Screen):
def teamNames(self):
select_screen = self.manager.get_screen("teamSelect")
self.teamOne_input.text = select_screen.ids.teamOne.text
self.teamTwo_input.text = select_screen.ids.teamTwo.text
Button:
text: 'Game On!'
on_release:
root.manager.current = 'startGame'
root.manager.current_screen.teamNames()

(Python,Kivy) when i convert app to exe, popup doesn't work

Pop up and EXE issue
Hi guys I've got problem, whenever I open my Kivy Program via Python, a popup is actually showing up
But when I convert it via Pyinstaller, it gives me an error:
How can I possibly fix this?
Python Code:
import os
os.environ["KIVY_IMAGE"] = "pil, sdl2"
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
Builder.load_file("kivy.kv")
class Rusure(Popup):
pass
class Menu(Screen):
def quit(self):
Rusure().open()
class Settings(Screen):
def bright (self,*args):
self.brightness.text = "Brightness: {}".format(str(int(args[1])))
class Play(Screen):
pass
class Sm(ScreenManager):
def __init__(self):
super().__init__()
self.add_widget(Menu(name="menu"))
self.add_widget(Settings(name="settings"))
self.add_widget(Play(name="play"))
class GoodApp(App):
def build(self):
return Sm()
GoodApp().run()
Kivy Code:
#: import rit kivy.uix.screenmanager.RiseInTransition
#: import st kivy.uix.screenmanager.SlideTransition
#: import fot kivy.uix.screenmanager.FallOutTransition
#: import Factory kivy.factory.Factory
<Menu>:
BoxLayout:
padding: 100,100,100,100
orientation: "vertical"
Label:
text: "Menu"
BoxLayout:
orientation: "horizontal"
Button:
text: "Play"
background_normal: "Image.jpg"
background_down: "ImageRel.jpg"
color: 1,1,1,1
on_press:
root.manager.transition = rit()
root.manager.current = "play"
Button:
text: "Settings"
background_color: 0.9,1,1,1
on_press:
root.manager.transition = st()
root.manager.current = "settings"
root.manager.transition.direction = "up"
Button:
text: "Quit"
background_color: 1,0.7,0,1
on_press:
root.quit()
<Settings>:
brightness: brght
BoxLayout:
padding: 100,100,100,100
orientation: "vertical"
Label:
text: "Settings"
GridLayout:
cols: 3
Label:
text: "Brightness"
Slider:
min: 0
max: 10
value: 5
on_value: root.bright(*args)
Label:
id: brght
text: "Change Value"
Button:
text: "Back"
on_press:
root.manager.transition = st()
root.manager.current = "menu"
root.manager.transition.direction = "down"
<Play>:
BoxLayout:
Label:
text: "Welcome to the game!"
Button:
text: "Back"
on_press:
root.manager.transition = fot()
root.manager.current = "menu"
<Rusure>:
title: "Are you sure?"
size_hint: 0.5,0.5
auto_dismiss: False
BoxLayout:
orientation: "vertical"
Label:
text: "Your progress will be lost"
GridLayout:
cols: 2
Button:
text: "Oh shit man!"
on_press: root.dismiss()
Button:
text: "Cancel"
on_press: root.dismiss()
I tried searching solution on the internet, but after 3 hours of searching I couldn't come up whith any answer.

How should I set a global variable in kivy Python?

My application is simple, the ScreenTwo is showing a form 'Parametreur' with various options to be entered.
What I'm trying to do is to set a 'save' button at the end of this form that will register into a list called 'resultat' all the options furfilled so far leaving '0' in the empty inputs.
The global variable 'resultat' would look like this ['blabla','15/06/2018','31/12/1999','6'].
File.py
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_file("HadrianRunningApp.kv")
class ScreenMenu(Screen):
pass
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class Parametreur(FloatLayout):
def build(self):
pass
def submit_input(self):
# Get the student name from the TextInputs
label_name = self.label_text_input.text
resultat.insert(0,label_name)
def submit_input2(self):
date_debut_name = self.date_debut_input.text
resultat.insert(1,date_debut_name)
def submit_input3(self):
date_fin_name = self.date_fin_input.text
resultat.insert(2,date_fin_name)
def submit_input4(self):
var_duree_estimation = self.duree_estimation_input.text
resultat.insert(3,var_duree_estimation)
def save_input(self):
print(resultat)
class Manager(ScreenManager):
screen_menu = ObjectProperty(None)
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=WipeTransition())
return m
if __name__=='__main__':
ScreensApp().run()
The Kivy file .kv:
#:kivy 1.9.0
<ScreenMenu>:
BoxLayout:
orientation:'vertical'
size: root.size
padding: "20dp"
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
<ScreenOne>:
BoxLayout:
spacing: 20
padding: 20
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
<ScreenTwo>:
Parametreur
<Parametreur>:
label_text_input: label_text
date_debut_input: date_debut
date_fin_input: date_fin
duree_estimation_input: duree_estimation
resultat: resultat
RelativeLayout:
orientation: 'vertical'
GridLayout:
size_hint: (1., 0.11)
pos_hint: {'right': 1, 'center_y': 0.91}
padding: 6
spacing: "4dp"
cols:2
rows:1
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
GridLayout:
size_hint: (1., 0.8)
pos_hint: {'right': 1, 'center_y': 0.45}
padding: "7dp"
spacing: 5
cols:2
rows:7
Button:
text: "Liste Actif"
Label:
text: " "
BoxLayout:
Label:
text: "Date du début"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input2()
TextInput:
id: date_debut
text: 'dd/mm/YYYY'
BoxLayout:
Label:
text: "Date de fin"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input3()
TextInput:
id: date_fin
text: 'dd/mm/YYYY'
Label:
text: "Pourcentage de séparation \n de la base (validation/test)"
Button:
text: "Open to Close"
Button:
text: "Close to Close"
Button:
text: "les 3 (6 en tout) frontières des VSs"
BoxLayout:
Label:
text: "Durée pour la réestimation \n des modèles (en jours)"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input4()
TextInput:
id: duree_estimation
text: 'dd/mm/YYYY'
Label:
text: "variable selection NMF/Entropy"
Label:
text: "Kernel/damiers/buntcher/\n neurone/XGBoost/Gradient boosting"
# We create the widgets
BoxLayout:
spacing: "0dp"
Label:
id: label_text
text: "0"
font_size: "30dp"
BoxLayout:
orientation: "vertical"
spacing: "3dp"
Button:
text: "Add"
on_release: label_text.text = str(int(label_text.text) + 1)
Button:
text: "Subtract"
on_release: label_text.text = str(int(label_text.text) - 1)
Button:
text: "Submit"
size_hint_x: None
on_press: root.submit_input()
BoxLayout:
padding: "2dp"
spacing: "2dp"
size_hint: (1., 0.50)
pos_hint: {'right': 1, 'center_y': 0.09}
Button:
id: resultat
text:"Save"
size_hint_x: None
on_press: root.save_input()
<Manager>:
id: screen_manager
screen_menu: screen_menu
screen_one: screen_one
screen_two: screen_two
ScreenMenu:
id: screen_menu
name: 'screen0'
manager: screen_manager
ScreenOne:
id: screen_one
name: 'screen1'
manager: screen_manager
ScreenTwo:
id: screen_two
name: 'Parametreur'
manager: screen_manager
Where and How should I set my global variable 'resultat' (which is a list)?
I've solved my problem but I still don't understand how does it work. Setting a variable is Kivy doesn't need to set it in the Python code. But how do Kivy and Python know about its type? Right now, it works as a list without me defining it such as (I intended it to be a list anyway).
How could I set a variable as tuple for example?

Categories