Click through button with opacity = 0 in Kivy and Python - python

is there any possibility in Kivy to make a button invisible, so that you run the on_press method of the button underneath when you click the first inviseible button?
Edit
class PlayGame(ButtonBehavior, Widget):
button = ObjectProperty(None)
def on_press(self):
do_something()
class PlayButton(Button):
def on_press(self):
if self.opacity == 1:
do_something()
elif self.opacity == 0:
return None
When I run this I can't click on the screen in the area where the button is (even when invisible) to run on_press() of PlayGame root widget.

Yes, make its opacity 0 and override its on_touch_down to do nothing (i.e. returning None, or you can return False if you like).

Related

is there anyway to create a confirmation popup like this

I am trying to make a popup that can return a true or false to where it was called from when a button is pressed
```python
import kivy
def confirmation_popup(title_text):
btn1 = kivy.uix.button.Button(text="Yes")
btn2 = kivy.uix.button.Button(text="no")
btn1.bind(on_release=return True)#to the function itself
btn2.bind(on_release=return False)
Boxed_layout= kivy.uix.boxlayout.BoxLayout(orientation = "horizontal")
Boxed_layout.add_widget(btn1)
Boxed_layout.add_widget(btn2)
pop = kivy.uix.popup.Popup(title=title_text,content=Boxed_layout)
pop.open()
if confirmation_popup("are you sure you want to delete your user"):
#delete user from database```
Rather than trying to get a return from the Popup, you should just let the Popup call a method that does the confirmed action. If you define your confirmation_popup like this:
def confirmation_popup(title_text, doit):
btn1 = Button(text="Yes")
btn2 = Button(text="no")
Boxed_layout= BoxLayout(orientation = "horizontal")
Boxed_layout.add_widget(btn1)
Boxed_layout.add_widget(btn2)
pop = Popup(title=title_text,content=Boxed_layout)
btn1.bind(on_release=partial(doit, pop)) # bind to whatever action is being confiirmed
btn2.bind(on_release=pop.dismiss)
pop.open()
# a method that does the action being confirmed
def doit(popup, button):
popup.dismiss()
print('do whatever action is confirmed')
Then you can use it by:
confirmation_popup('This is a Test', doit)

How to toggle fullscreen mode?

I want my application to toggle fullscreen every time you click on the menu item. So if you click once, it becomes fullscreen, if you click again, it becomes normal again. I tried the following but after I clicked it again, it wouldn't switch.
def Fullscreen(self):
self.fullscreen = False
if not self.fullscreen:
self.root.wm_attributes("-fullscreen", True)
else:
self.root.wm_attributes("-fullscreen", False)
You are missing a key part here. Nothing changes full screen back to True.
Here is a simple example of what you could do to toggle full screen.
import tkinter as tk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Button(self, text="Toggle Fullscreen", command=self.fullscreen_toggle).pack()
self.fullscreen = False
def fullscreen_toggle(self):
if self.fullscreen == False:
self.wm_attributes("-fullscreen", True)
self.fullscreen = True
else:
self.wm_attributes("-fullscreen", False)
self.fullscreen = False
app = App()
app.mainloop()

python kivy generating buttons linked to jsonstore items in a for loop

I'm using a for loop to generate a button for every key in a jsonstore.
The buttons are all added to the layout correctly and the button.text is correct but when the button calls the callback, they all link to the same key. (the last key)
here is the code:
def show_saved_conditions(*args,**kwargs):
self.label7 = Label(text = str(self.store[self.btns.text], size_hint = (.3,.3), pos_hint = {'x':.3,'y':.5}, color = (0,0,1,1)))
self.layout.add_widget(self.label7)
def view_saved_conditions(*args, **kwargs):
x = 0
y = 0
for i in self.store.keys():
self.btns = (Button(text = i, size_hint = (.2,.1), pos_hint = {'x':x,'y':y}, on_release = show_saved_conditions))
self.layout.add_widget(self.btns)
x +=.2
if x >= 1:
y+=.1
x = 0
pretty sure this question has been asked before but i was unable to find a post specific enough for me to relate to.
Thank you in advance...
Problem - Always refer to the last button
In show_saved_conditions() method, it is always using self.btns.text which is the last button added to the layout.
self.label7 = Label(text = str(self.store[self.btns.text], size_hint = (.3,.3), pos_hint = {'x':.3,'y':.5}, color = (0,0,1,1)))
Solution
In the example, it demonstrates on_touch_down event.
Touch event basic
By default, touch events are dispatched to all currently displayed
widgets. This means widgets receive the touch event whether it occurs
within their physical area or not.
In order to provide the maximum flexibility, Kivy dispatches the
events to all the widgets and lets them decide how to react to them.
If you only want to respond to touch events inside the widget, you
simply check:
def move(self, touch):
if self.collide_point(*touch.pos):
# The touch has occurred inside the widgets area. Do stuff!
pass
Example
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class CreateButton(Button):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
if touch.button == "right":
print(self.id, "right mouse clicked")
elif touch.button == "left":
print(self.id, "left mouse clicked")
else:
print(self.id)
return True
return super(CreateButton, self).on_touch_down(touch)
class OnTouchDownDemo(GridLayout):
def __init__(self, **kwargs):
super(OnTouchDownDemo, self).__init__(**kwargs)
self.build_board()
def build_board(self, *args):
# make 9 buttons in a grid
for i in range(0, 9):
button = CreateButton(id=str(i), text=str(i))
self.add_widget(button)
class OnTouchDownApp(App):
def build(self):
return OnTouchDownDemo()
if __name__ == '__main__':
OnTouchDownApp().run()
ontouchdown.kv
#:kivy 1.11.0
<CreateButton>:
font_size: 50
<OnTouchDownDemo>:
rows: 3
cols: 3
row_force_default: True
row_default_height: 150
col_force_default: True
col_default_width: 150
padding: [10]
spacing: [10]
Output
The *args argument to your show_saved_conditions() method contains the Button instance that was pressed. So that method could be:
def show_saved_conditions(self, btn_instance):
self.label7 = Label(text = str(self.store[btn_instance.text], size_hint = (.3,.3), pos_hint = {'x':.3,'y':.5}, color = (0,0,1,1)))
self.layout.add_widget(self.label7)
Since you used self in the method, I have assumed that this is an instance method and the correct first arg is self. If it is not an instance method, then just remove the self arg, but then where does the method get the value for self?
Of course, this method will overwrite self.label7 each time that it is executed.

Bind a function to multiple dynamically created buttons in kivy?

Problem
I want to create multiple buttons and bind them to a function. The problem is that whenever I click on one button, the function is called multiple times. It seems to be a problem with the event connection. When I look at the instance that called the function when I pressed a button, it seems that the function gets called from every button at once?!
KV Code:
...
# This is the button that I'am using
<ProjectSelectButton>:
height: 35
background_color: 0,0,1,1
on_touch_down: self.click_on_button(args[0], args[1])
...
# The buttons are added to this grid
ButtonsPlacedHere#GridLayout:
id: active_projects
cols: 1
size_hint_y: None
height: self.minimum_height
spacing: 1
...
Python Code:
...
class ProjectSelectButton(Button):
def click_on_button(self, instance, touch, *args):
print(instance)
if touch.button == 'right':
print(self.id, "right mouse clicked")
else touch.buttom == 'left':
print(self.id, "left mouse clicked")
...
# The part of my programm that creates the buttons
# projects is a dictionary
for key, project in data_manager.resource_pool.projects.items():
print(project.project_name)
button= ProjectSelectButton(text=project.project_name, id=key, size_hint_y=None)
# Bind without KV-File (same result)
# label.bind(on_touch_up=self.click_on_button)
ids.active_projects.add_widget(button)
Example Output:
What I get, when I click on a single button!
<guiMain.ProjectSelectButton object at 0x0BA34260>
ID01 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA34260>
ID01 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA28F10>
ID02 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA28F10>
ID02 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA22C00>
ID03 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA22C00>
ID03 right mouse clicked
What I want when I press for example the button with ID 01:
<guiMain.ProjectSelectButton object at 0x0BA34260>
ID01 right mouse clicked
Question
How do I create multiple buttons which will call a single function when they are pressed?
Programming Guide » Input management » Touch events
By default, touch events are dispatched to all currently displayed widgets. This means widgets receive the touch event whether it occurs within their physical area or not.
In order to provide the maximum flexibility, Kivy dispatches the
events to all the widgets and lets them decide how to react to them.
If you only want to respond to touch events inside the widget, you
simply check.
Solution
Use self.collide_point method in click_on_button method. When it collides, you should get only one button. Please refer to my example for details.
Snippets
class ProjectSelectButton(Button):
def click_on_button(self, instance, touch, *args):
print(instance)
if self.collide_point(*touch.pos):
if touch.button == 'right':
print(self.id, "right mouse clicked")
elif touch.buttom == 'left':
print(self.id, "left mouse clicked")
return True
return super(ProjectSelectButton, self).on_touch_down(touch)
Example
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class CreateButton(Button):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
if touch.button == "right":
print(self.id, "right mouse clicked")
elif touch.button == "left":
print(self.id, "left mouse clicked")
else:
print(self.id)
return True
return super(CreateButton, self).on_touch_down(touch)
class OnTouchDownDemo(GridLayout):
def __init__(self, **kwargs):
super(OnTouchDownDemo, self).__init__(**kwargs)
self.build_board()
def build_board(self):
# make 9 buttons in a grid
for i in range(0, 9):
button = CreateButton(id=str(i))
self.add_widget(button)
class OnTouchDownApp(App):
def build(self):
return OnTouchDownDemo()
if __name__ == '__main__':
OnTouchDownApp().run()
ontouchdown.kv
#:kivy 1.10.0
<CreateButton>:
font_size: 50
on_touch_down: self.on_touch_down
<OnTouchDownDemo>:
rows: 3
cols: 3
row_force_default: True
row_default_height: 150
col_force_default: True
col_default_width: 150
padding: [10]
spacing: [10]

PyGTK how to click button and open file in pop-up window

I editted the code so that the 3 buttons now show up .Can someone please tell me how to make it so that when I click the button that says Helloworld and application called Helloworld.py will pop-up in another window.Same for the other 2 buttons
!/usr/bin/env python
# menu.py
import pygtk
pygtk.require('2.0')
import gtk
class MenuExample:
def __init__(self):
# create a new window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200, 100)
window.set_title("GTK Menu Test")
window.connect("delete_event", lambda w,e: gtk.main_quit())
# Init the menu-widget, and remember -- never
# show() the menu widget!!
# This is the menu that holds the menu items, the one that
# will pop up when you click on the "Root Menu" in the app
menu = gtk.Menu()
# Next we make a little loop that makes three menu-entries for
# "test-menu". Notice the call to gtk_menu_append. Here we are
# adding a list of menu items to our menu. Normally, we'd also
# catch the "clicked" signal on each of the menu items and setup a
# callback for it, but it's omitted here to save space.
for i in range(3):
# Copy the names to the buf.
buf = "Test-undermenu - %d" % i
# Create a new menu-item with a name...
menu_items = gtk.MenuItem(buf)
# ...and add it to the menu.
menu.append(menu_items)
# Do something interesting when the menuitem is selected
menu_items.connect("activate", self.menuitem_response, buf)
# Show the widget
menu_items.show()
# This is the root menu, and will be the label
# displayed on the menu bar. There won't be a signal handler attached,
# as it only pops up the rest of the menu when pressed.
root_menu = gtk.MenuItem("Root Menu")
root_menu.show()
# Now we specify that we want our newly created "menu" to be the
# menu for the "root menu"
root_menu.set_submenu(menu)
# A vbox to put a menu and a button in:
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
# Create a menu-bar to hold the menus and add it to our main window
menu_bar = gtk.MenuBar()
vbox.pack_start(menu_bar, False, False, 2)
menu_bar.show()
# Create a button to which to attach menu as a popup
button = gtk.Button("HelloWorld")
button.connect_object("event", self.button_press, menu)
vbox.pack_end(button, True, True, 2)
button.show()
button2 = gtk.Button("Scrible")
button2.connect_object("event", self.button_press, menu)
vbox.pack_end(button2, True, True, 2)
button2.show()
button3 = gtk.Button("Final")
button3.connect_object("event", self.button_press, menu)
vbox.pack_end(button3, True, True, 2)
button3.show()
# And finally we append the menu-item to the menu-bar -- this is the
# "root" menu-item I have been raving about =)
menu_bar.append (root_menu)
# always display the window as the last step so it all splashes on
# the screen at once.
window.show()
# Respond to a button-press by posting a menu passed in as widget.
#
# Note that the "widget" argument is the menu being posted, NOT
# the button that was pressed.
def button_press(self, widget, event):
if event.type == gtk.gdk.BUTTON_PRESS:
widget.popup(None, None, None, event.button, event.time)
# Tell calling code that we have handled this event the buck
# stops here.
return True
# Tell calling code that we have not handled this event pass it on.
return False
def button2_press(self, widget, event):
if event.type == gtk.gdk.BUTTON2_PRESS:
widget.popup(None, None, None, event.button, event.time)
return True
return False
def button3_press(self, widget, event):
if event.type == gtk.gdk.BUTTON3_PRESS:
widget.popup(None, None, None, event.button, event.time)
return True
return False
# Print a string when a menu item is selected
def menuitem_response(self, widget, string):
print "%s" % string
def main():
gtk.main()
return 0
if __name__ == "__main__":
MenuExample()
main()
You could do something like this. I'm assuming you just want to execute your .py files, e.g. helloworld.py etc. I'm using Popen from subprocess to execute python (not assuming the py files are executable) scripts. Note that I've edited the script to only have one button, this is just to show you the idea.
import pygtk
pygtk.require('2.0')
import gtk
import subprocess
class Example:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200, 100)
window.set_title("GTK Menu Test")
window.connect("delete_event",
lambda w,e: gtk.main_quit())
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
button = gtk.Button("HelloWorld")
button.connect("clicked", self.clicked_helloworld)
vbox.pack_end(button, True, True, 2)
button.show()
window.show_all()
def clicked_helloworld(self, widget):
subprocess.Popen(["python", "helloworld.py"])
def main(self):
gtk.main()
return 0
Example().main()

Categories

Resources