I am working on developing an application using Kivy. I am using Kivy ActionBar for creating a menu bar for my application.
Kindly refer to the image attached
I want to remove the Kivy icon and move the other options(file / edit) to the left. Please find the snippet of my code.
menuAcBar = ActionBar(pos_hint={'top': 1.3})
menuAcView = ActionView()
menuAcBar.add_widget(menuAcView)
menuAcPrevious = ActionPrevious(with_previous=False)
menuAcView.add_widget(menuAcPrevious)
menuAcView.add_widget(ActionButton(text="File"))
menuAcView.add_widget(ActionButton(text="Edit"))
menuAcView.add_widget(ActionButton(text="Documents"))
menuAcView.add_widget(ActionButton(text="help"))
self.add_widget(menuAcBar)
Right on ActionPrevious you can set app_icon. It's a little bit lower in docs. You can set app_icon_width/height for size of the icon or even remove it with app_icon='', but it'll leave white rectangle instead of a "transparent". Leave app_icon be and set only width and height to make it invisible.
The Ä„ctionPrevious has ActionItem's minimum_width property, therefore you need to change it like this:
menuAcPrevious = ActionPrevious(with_previous=False,
app_icon=<your_image>,
app_icon_width=1,
app_icon_height=0,
minimum_width=10,
size_hint_x: None)
Edit:
It seems that ActionPrevious leaves additional unused space even if title='' and minimum_width=1 and you can't access the damn thing through children because it's unregistered, therefore the only thing I came up with is resizing it so you won't see it anymore:
ActionPrevious(
size_hint_x = None,
width = 0,
app_icon_width = 0.1,
with_previous = False)
Related
I want to change the MDToolbar left_action_item icon color. Its defaulting to white, but now i want to change it to red. Whats the simplest way to do this? I've tried almost everything (text_color, bg_color, etc) all to no avail.
You cannot change the color of the icons in the toolbar.
Using specific_text_color: 1,0,1,1 you can change the color of the text inside the toolbar. It changes both the text AND the icon. I have no idea how to change only the icon. Maybe this helps.
At the moment iam having trouble changing the icon color of a OneLineIconListItem. I think its the same constraint we are encountering?
In situations like these, I recommend searching the KivyMD repository for the relevant widget class, and then poking around to see how it's being defined, what the relevant IDs are, and so forth. For instance, this line in toolbar.py seems to define the icons in the toolbar:
def update_action_bar(self, action_bar, action_bar_items):
#...
action_bar.add_widget(
MDIconButton(
icon=item[0],
on_release=item[1],
opposite_colors=True,
text_color=self.specific_text_color,
theme_text_color="Custom",
)
)
#...
Here we learn that the toolbar's icons are of class MDIconButton, and they have a text_color color attribute which seems to be setting the color.
Looking at where the function above is called, we see that these icons are being added as widgets to self.ids["left_actions"] and self.ids["right_actions"] respectively:
def on_left_action_items(self, instance, value):
self.update_action_bar(self.ids["left_actions"], value)
def on_right_action_items(self, instance, value):
self.update_action_bar(self.ids["right_actions"], value)
Knowing all that, now in our own code, say in the build() function of our MainApp, we can access and modify the attribute:
def build(self):
# ...
# get the root widget
self.root = root = Builder.load_file('root.kv')
# get toolbar
toolbar=root.ids.toolbar
# get the icons on the right
action_items = toolbar.ids.right_actions.children
# loop over the icons
for item in action_items:
# change the color
item.text_color=(1,0,0,1) # red
This doesn't need to be in build(), it just needs to be somewhere you can access the toolbar widget by its ID somehow.
Using both md_bg_color: app.theme_cls.primary_color and text_color: rgba('#F0F0F0') allowed me to change the color of icon buttons within MDToolbar.
Lets say we have a gridlayout that is 2 columns.
We then add something like this:
gridLayout = GridLayout(cols = 2, orientation = "horizontal")
button = Button(text = "Hello")
label = Label(text = "World", size_hint = (.1,.1))
gridLayout.add_widget(button)
gridLayout.add_widget(label)
I need the 2 columns, but I pretty much want the button to have most of the screen and the label to just be a very small placeholder.
I have tried doing size_hint's on both and just not able to change the size of it.
I am using Python 2.7.x and Kivy 1.8 +
Thanks.
This code seems to work for me, but perhaps you need to use the width/height directly, for which you'll need to set size_hint = (None, None). You might be interested even in texture_size.
Just a note, if you use Kivy 1.8 or basically anything that's below the latest stable version (now 1.9.1), it's the best time to update Kivy asap.
I'm building a Python GUI application using ttk treeviews. On Linux, when a Treeitem has child items, it displays an arrow to show that the row can be expanded. I want to hide this indicator arrow (I am using other ways to hint that the row can be expanded). How can I do this?
If I run Style().element_names() I see that there's a Treeview element and a Treeitem.indicator element. If I run Style().configure("Treeview", padding=50), I see the padding style get applied when I create the treeview, so I feel confident that any style I correctly apply to Treeitem.indicator should be visible also.
Running Style().element_options("Treeitem.indicator"), I see ('-foreground', '-indicatorsize', '-indicatormargins'). Running Style().lookup("Treeitem.indicator", "foreground") gives me "#000000", so it appears that value is initialized. If I try Style().configure("Treeview", foreground="#123456") I don't see the indicator arrow change color, though running Style.lookup("Treeitem.indicator", "foreground") shows me "#123456" as expected. My plan was to set the indicatorsize to 0 to make the arrow go away entirely, but I cannot even successfully edit the color. What am I doing wrong here and is there a better way to hide the indicator? In case it matters, I'm running Python 3.5.0.
Not sure if you ever figured it out.
When you create the new style and configure it, you have to change the name of the template to ".". This changes the root style for the treeview. You also need to specify a theme, even if it is default. So it would look something like:
s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('default')
Then when you create the treeview, you shouldn't have to specify a style at all.
Let me know if this works for you.
Edit: Since this is being downvoted for some reason, I'll clarify:
Code with the style part commented out:
#s = ttk.Style()
#s.configure(".", indicatorsize = '0')
#s.theme_use('clam')
j = ttk.Treeview(self.parent)
j.place(relx = 0.5, rely = 0.5, anchor = "center")
j.insert("",1,"jacob",text = "Jacob")
j.insert("jacob",1,"marcus",text = "Marcus")
j.insert("jacob",2,"tony",text = "Tony")
j.insert("jacob",3,"ricardo",text = "Ricardo")
gives us
Code with the style part present
s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('clam')
j = ttk.Treeview(self.parent)
j.place(relx = 0.5, rely = 0.5, anchor = "center")
j.insert("",1,"jacob",text = "Jacob")
j.insert("jacob",1,"marcus",text = "Marcus")
j.insert("jacob",2,"tony",text = "Tony")
j.insert("jacob",3,"ricardo",text = "Ricardo")
Hope this helps.
EDIT 2:
Added the s.theme_use('clam') line because you need to specify which theme you're using. It also works with classic and default, but for some reason doesn't work with the vista theme.
I am making a BoxLayout widget (orientation = 'horizontal') that contains three widgets inside of it, a label, a text box, and a check box.
thisRow = BoxLayout(orientation='horizontal')
l = Label(text='Enter plate 1:\n(Plate #)')
t = TextInput(text = 'this is a text box')
c = CheckBox()
thisRow.add_widget(l)
thisRow.add_widget(t)
thisRow.add_widget(c)
This produces the following widget (thisRow):
After the box is checked...
The rightmost black box is actually the checkbox, and works functionally, however there is no way for the user to know that it is in fact a checkbox. I would expect a smaller empty square in the middle, as is depicted in pictures here.
How do i get the traditional checkbox image (smaller empty square box)? Or generally, how can I make it more obvious that the box is a check box and not just an empty label?
Thank you
This is really interesting question and Malonge tried it in a good way. Right now(1.9.2-dev) there is still fixed size on CheckBox's well, call it a background. It's an image that Widget takes from atlas and changes if the state changes. Therefore until now there was no clear way how to do it. Here is an example. Soon on master there'll be CheckBox(color=[r,g,b,a]) option. Thanks ;)
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<CheckBoxBG>:
Label:
TextInput:
CheckBox:
canvas.before:
Color:
rgb: 1,0,0
Rectangle:
pos:self.center_x-8, self.center_y-8
size:[16,16]
Color:
rgb: 0,0,0
Rectangle:
pos:self.center_x-7, self.center_y-7
size:[14,14]
''')
class CheckBoxBG(BoxLayout):pass
runTouchApp(CheckBoxBG())
Looks like the smaller check box is hidden when the background color is black. Here is an example of a red background.
It's not ideal because I do like the black background, but I can run with it for now. If anyone knows how to do this with a black background that would be great. Thank you
Alternatively, to change your checkboxes background, you can use another image from the atlas or create images and then load them:
mycheckbox= CheckBox(
background_checkbox_normal ='tlas://data/images/defaulttheme/button_disabled'
background_checkbox_down = 'my_checkboxes_checked.png'
)
In Kivy 1.9.2.dev0 (and apparently since version 1.9.0) you can change the background image of checkboxes. By default, Kivy uses for these backgrounds from the atlas*.
background_checkbox_normal = StringProperty('atlas://data/images/defaulttheme/checkbox_off') #when the checkbox is not active.
background_checkbox_down = StringProperty('atlas://data/images/defaulttheme/checkbox_on') # when the checkbox is active.
background_checkbox_disabled_normal = StringProperty('atlas://data/images/defaulttheme/checkbox_disabled_off') #when the checkbox is disabled and not active.
background_checkbox_disabled_down = StringProperty('atlas://data/images/defaulttheme/checkbox_disabled_on') #when the checkbox is disabled and active.
You can have a look here at all the attributes :
*The atlas is a package of multiple textures that reduces the number of images loaded and speedup the application loading. You have see a preview of the atlas in Python Install Folder\Lib\site-packages\kivy\data\images\defaulttheme-0.png
I'm trying to write a very basic Kivy program that will use 3 different layouts to split the screen into :
a header (at the top of the screen)
a text zone (in the middle of the screen)
a console (at the bottom of the screen)
So far I was thinking to use a main gridLayout, in which I use 3 different floatLayout.
Here's what the code looks like:
class Logo(App):
def build(self):
layout = GridLayout(rows=3)
layoutTop = FloatLayout(size=(100,300))
layoutMid = FloatLayout(size=(100,300))
layoutDown = FloatLayout(size=(100,300))
logo = Image(source='imagine.png',size_hint=(.25,.25),pos=(30,380))
blank = Label(text='', font_size = '25sp',pos=(-200,100))
titre = Label(text='#LeCubeMedia',font_size='40sp',pos=(0,280))
ip = Label(text='192.168.42.1',font_size='25sp',pos=(250,280))
layoutTop.add_widget(titre)
layoutTop.add_widget(logo)
layoutTop.add_widget(ip)
layoutMid.add_widget(blank)
layout.add_widget(layoutTop)
layout.add_widget(layoutMid)
return layout
if __name__ == '__main__':
Logo().run()
Actually my problem is regarding the creation of the console. I have read a lot of the Kivy docs, but I am still looking for a good way to do this type of widget.
How do you think it would be if I send something with a Python print into my Kivy app, and then refresh as soon as I need to send something else (to erase the previous print). This way it would be a console-like. But, so far I have not much ideas..
Any ideas ?
I have seen 2 types of consoles in Kivy. The first is a multiline textinput in a scrollview where you append the new text to the old in the textinput. The second is a BoxLayout or GridLayout in a Scrollview where each console output is a separate label in the layout.
This was a attempt trying stuff out with kivy, the code is old and you might need to adjust it a bit to make it run with latest kivy. Kivy-designer also includes this. This is using the simple way of using two textinputs, 1 for history and the other for input.
A better way to do a proper console would be to use pyte and draw characters directly onto the canvas of a widget. This way one would get VT emulation for free.