I have a rather large App split in a single .kv file and a GUI.py file accessing a library of other .py files. I came to a point where i want to organize everything and split up large layouts into different classes and .kv files.
Currently im working on a function which should add and remove a certain layout to my main layout while still accessing variables and functions of the base class (called BoxL). I tried various things, but i dont know how i can hand over/instantiate my main class to/in my new class.
Im trying to build a rough minimal example:
Main python file: GUI.py
import kivy
from kivy.app import App
from kivy.config import Config
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class AdvancedClass(BoxLayout):
"""This is my new class from which i want to access BoxL class."""
pass
class BoxL(BoxLayout):
def __init__(self):
super(BoxL, self).__init__()
some_variable = 1
advanced_mode_enabled = False
def some_function(self):
print(self.some_variable)
print('Im also doing stuff in GUI.kv file with ids')
self.ids.test_label.text = '[b]some text with markup'
def advanced_mode(self):
if not self.advanced_mode_enabled:
print('Enabling advanced mode ..')
self.ids.master_box.add_widget(self.advanced_class_obj)
self.advanced_mode_enabled = True
else:
print('Disabling advanced mode ..')
self.ids.master_box.remove_widget(self.advanced_class_obj)
self.advanced_mode_enabled = False
class GUI(App):
def build(self):
Builder.load_file('layouts.kv') # i read its best to instanciate kivy files here once everything is loaded
BoxL.advanced_class_obj = AdvancedClass()
if __name__ == "__main__":
GUI().run()
Main layout file: GUI.kv
<BoxL>:
orientation: 'vertical'
id: master_box
Label:
text: str(root.some_variable)
Button:
text: 'Change variable'
on_release:
root.some_variable = 2
Button:
text: 'Add advanced layout'
on_release:
root.advanced_mode(self)
New layout file layouts.kv from which i want to access functions/variables of BoxL class in GUI.py:
<AdvancedClass>:
orientation: 'vertical'
Label:
text: '[b]TEST'
TextInput:
hint_text: 'TEST'
Button:
text: 'KLICK'
on_release:
# print(parent.some_variable)
# print(self.some_variable)
# print(BoxL.some_variable)
print('Im not sure how to do this .. ') # issue-point
I hope this covers everything. Im struggling with this for quite a while.
Figured it out: app.root.FUNCTION()
Related
Here is a minimal GTK 4 app written using the Python bindings, based on this tutorial. It displays a window with a very long block of text:
import sys
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
long_label = Gtk.Label.new("Here is a very long chunk of text.\n" * 100)
self.set_child(long_label)
class MyApp(Adw.Application):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.connect('activate', self.on_activate)
def on_activate(self, app):
self.win = MainWindow(application=app)
self.win.present()
if __name__ == "__main__":
app = MyApp(application_id="com.example.GtkApplication")
app.run(sys.argv)
The tutorial uses the class Gtk.ApplicationWindow for the MainWindow, which lets you set the application_id in the main function and other convenient features.
However, the default Gtk.ApplicationWindow does not include a scrollbar. Rather than build one manually, it seems the best way to do this is to make my main window an instance of Gtk.ScrolledWindow, documented here, which lets you specify the scroll behavior etc. at a high level.
Unfortunately, dropping Gtk.ScrolledWindow in place of Gtk.ApplicationWindow causes an error:
TypeError: gobject `__main__+MainWindow' doesn't support property `application'
Is there a way to create a Gtk.ApplicationWindow that is also a Gtk.ScrolledWindow?
If not, what is the best way to get a scrollbar in my main window?'
(I have added the pygobject tag because it is the language of my MWE, but I think that my question is agnostic with respect to the binding language.)
A Gtk.ScrolledWindow isn't really a "window" per se as most people know it (it used to map to the very specific concept of a X11 Window). It's a widget that allows you to scroll through its child widget.
In other words, if you want an application window where you can scroll through the content, you would wrap your Gtk.Label with a Gtk.ScrolledWindow and you would then set that as the Gtk.ApplicationWindow's child.
In other words, your MainWindow would become:
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
long_label = Gtk.Label(label=("Here is a very long chunk of text.\n" * 100))
# Wrap a scrolled window around it
scrolled = Gtk.ScrolledWindow()
scrolled.set_child(long_label)
# Set it as the main child of the window
self.set_child(scrolled)
I have a form created with PyQt5 Designer. Within this form, I have a QPushButton and a QWidget. I want to draw shapes on the QWidget with QPainter. I want to draw on the QWidget (named drawing_area) with custom methods such as my_rectangle(), my_line(), etc. I also want to trigger a custom function clear_drawings() to clean the drawings on the widgets by a button click. Following is my UI. I couldn't figure out how to target a certain QWidget within a form. I appreciate guidance here.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(770, 347)
self.drawButton = QtWidgets.QPushButton(Form)
self.drawButton.setGeometry(QtCore.QRect(20, 60, 91, 24))
self.drawButton.setObjectName("drawButton")
self.drawing_area = QtWidgets.QWidget(Form)
self.drawing_area.setGeometry(QtCore.QRect(280, 30, 381, 191))
self.drawing_area.setAutoFillBackground(False)
self.drawing_area.setObjectName("drawing_area")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.drawButton.setText(_translate("Form", "Click to Draw!"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
I am developing a QGIS plugin in python and hit a roadblock when displaying my GUI. I am using the plugin builder framework to develop my plugin and I have trouble displaying a checkable combo box in a scrollArea in my GUI. The code with core functionality is as follows.
def run(self):
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
# Sets up the pyqt user interface
self.dlg = EarthingToolDialog()
# Fetching the active layer in the QGIS project
layer = self.iface.activeLayer()
checkable_combo = CheckableComboBox()
# Going through each field of the layer
# and adding field names as items to the
# combo box
for j,field in enumerate(layer.fields()):
checkable_combo.addItem(str(field.name()))
# Setting the checked state to True by default
checkable_combo.setItemChecked(j, True)
# putting the check box inside the scroll area of the GUI
self.dlg.scrollArea.setWidget(checkable_combo)
self.dlg.scrollArea.setMinimumSize(QSize(700,400))
# show the dialog
self.dlg.show()
# Run the dialog event loop
self.dlg.exec_()
class EarthingToolDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(EarthingToolDialog, self).__init__(parent)
self.setupUi(self)
class CheckableComboBox(QComboBox):
def __init__(self):
super().__init__()
self._changed = False
self.view().pressed.connect(self.handleItemPressed)
def setItemChecked(self, index, checked=False):
print('checked')
item = self.model().item(index, self.modelColumn()) # QStandardItem object
print(type(item))
if checked:
item.setCheckState(Qt.CheckState.Checked)
else:
item.setCheckState(Qt.CheckState.Unchecked)
def handleItemPressed(self, index):
print('pressed')
item = self.model().itemFromIndex(index)
if item.checkState() == Qt.Checked:
item.setCheckState(Qt.Unchecked)
else:
item.setCheckState(Qt.Checked)
self._changed = True
print('set ' + str(item.checkState()))
def hidePopup(self):
print('hide')
if not self._changed:
super().hidePopup()
self._changed = False
def itemChecked(self, index):
print('read')
item = self.model().item(index, self.modelColumn())
return item.checkState() == Qt.Checked
In summary, the run function is the main function called by the plugin when it is loaded. self.dlg is the instance of the actual pyqt python user interface. This is rendered with the help of the EarthingToolDialog class. The checkable combo box and it's functionalities are self contained in the CheckableComboBox class.
The run function executes without any error when the plugin is loaded but the checkboxes are not visible in the combobox. Just a normal combo box with a list of items (just the standard dropdown combo box) is seen on the GUI's scroll area and not the desired checkable combo box. The CheckableComboBox class was taken from https://morioh.com/p/d1e70112347c and it runs perfectly well in the demo code shown there.
I understand that this is a very specific question and it would be great if someone could figure out what the problem might be. Thanks in advance!
Within the run function, this piece of codes didn't work for me:
self.dlg.scrollArea.setWidget(checkable_combo)
self.dlg.scrollArea.setMinimumSize(QSize(700,400))
So instead, I use:
layout = QVBoxLayout()
layout.addWidget(checkable_combo)
self.dlg.setLayout(layout)
I didn't use directly this class (It was generated automatically since I use Plugin Builder, so in here I commented it):
class EarthingToolDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(EarthingToolDialog, self).__init__(parent)
self.setupUi(self)
Now, in order to display checkable combo box, CheckableComboBox constructor is changed as :
def __init__(self):
super().__init__()
self._changed = False
self.view().pressed.connect(self.handleItemPressed)
delegate = QtWidgets.QStyledItemDelegate(self.view())
self.view().setItemDelegate(delegate)
The last two lines are from the answer listed here.
Codes display checkable combo box list with all items checked by default.
I am following the example code I've attached below. It currently displays the plot and associated slider embedded in the doc returned by the "modify_doc" function in the notebook. However, I'd like to deploy it into it's own server to make a GUI, while still maintaining it's ability to run the callback when the slider is changed and update the plot. However when I try to use Panel Pyviz to deploy it, it just displays the message "< bokeh.document.document.Document object at 0x00000193EC5FFE80 >" on the server that pops up. How do I deploy the doc in a way that will display the image?
import numpy as np
import holoviews as hv
from bokeh.io import show, curdoc
from bokeh.layouts import layout
from bokeh.models import Slider, Button
renderer = hv.renderer('bokeh').instance(mode='server')
# Create the holoviews app again
def sine(phase):
xs = np.linspace(0, np.pi*4)
return hv.Curve((xs, np.sin(xs+phase))).opts(width=800)
stream = hv.streams.Stream.define('Phase', phase=0.)()
dmap = hv.DynamicMap(sine, streams=[stream])
# Define valid function for FunctionHandler
# when deploying as script, simply attach to curdoc
def modify_doc(doc):
# Create HoloViews plot and attach the document
hvplot = renderer.get_plot(dmap, doc)
# Create a slider
def slider_update(attrname, old, new):
# Notify the HoloViews stream of the slider update
stream.event(phase=new)
start, end = 0, np.pi*2
slider = Slider(start=start, end=end, value=start, step=0.2, title="Phase")
slider.on_change('value', slider_update)
# Combine the holoviews plot and widgets in a layout
plot = layout([
[hvplot.state],
[slider]], sizing_mode='fixed')
doc.add_root(plot)
return doc
# To display in the notebook
show(modify_doc, notebook_url='localhost:8888')
# To display in a script
doc = modify_doc(curdoc())
# To deploy to separate server using Panel (attempt, doesn't work. Just displays #"<bokeh.document.document.Document object at 0x00000193EC5FFE80>":
graph = pn.Row (doc)
graph.show()
There is some mixing of very different APIs going on in your example. Panel is meant to simplify some of Bokeh's APIs so a lot of what you're doing here isn't necessary. That said I'll provide a few versions which are either using only Panel components or combining Panel and bokeh components.
To start with this is how I might write this example using just Panel:
import numpy as np
import panel as pn
import holoviews as hv
pn.extension()
start, end = 0, np.pi*2
slider = pn.widgets.FloatSlider(start=start, end=end, value=start, step=0.2, name="Phase")
#pn.depends(phase=slider.param.value)
def sine(phase):
xs = np.linspace(0, np.pi*4)
return hv.Curve((xs, np.sin(xs+phase))).opts(width=800)
dmap = hv.DynamicMap(sine)
row = pn.Row(dmap, slider)
# Show in notebook
row.app('localhost:8888')
# Open a server
row.show()
# To deploy this using `panel serve` or `bokeh serve`
row.servable()
In your example you instead use various bokeh components, this is also possible and might be desirable if you've already got bokeh code.
import numpy as np
import holoviews as hv
import panel as pn
from bokeh.models import Slider, Button
# Create the holoviews app again
def sine(phase):
xs = np.linspace(0, np.pi*4)
return hv.Curve((xs, np.sin(xs+phase))).opts(width=800)
stream = hv.streams.Stream.define('Phase', phase=0.)()
dmap = hv.DynamicMap(sine, streams=[stream])
def slider_update(attrname, old, new):
# Notify the HoloViews stream of the slider update
stream.event(phase=new)
start, end = 0, np.pi*2
slider = Slider(start=start, end=end, value=start, step=0.2, title="Phase")
slider.on_change('value', slider_update)
graph = pn.Row(dmap, slider)
# Show in notebook
row.app('localhost:8888')
# Open a server
row.show()
# To deploy this using `panel serve` or `bokeh serve`
row.servable()
If you want to serve these apps to multiple people I'd definitely recommend using panel serve but if you really want to make a script you can run with python script.py you should do this:
def app():
start, end = 0, np.pi*2
slider = pn.widgets.FloatSlider(start=start, end=end, value=start, step=0.2, name="Phase")
#pn.depends(phase=slider.param.value)
def sine(phase):
xs = np.linspace(0, np.pi*4)
return hv.Curve((xs, np.sin(xs+phase))).opts(width=800)
dmap = hv.DynamicMap(sine)
return pn.Row(dmap, slider)
pn.serve({'/': app})
This requires latest Panel, but ensures that even if you run the app as a script that each user gets a new instance of the app which does not share state with all others.
I'd like to have a GtkAppChooserButton which allows the user to select a program to run which will most likely want to be an audio mixer such as pavucontrol. Despite vague documentation on the matter I gather the app chooser's content type is meant to be a MIME type, however I cannot find a suitable MIME type for an audio mixer, or more generally just "all applications".
Some types such as application/ will give two Other Application... options if Other... items are enabled, both of which are identical and neither of which contain half the applications I have, including any audio mixers. Aside from that, nothing else I do gets me remotely close to what I'm after.
Is there a MIME type and/or GtkAppChooser content type (they seem to be the same thing?) for audio mixers or just all programs in general? (I.e. Any program that would have an icon in the likes of the Gnome app launcher/xfce4-whisker-menu/etc.)
Alright so I've come up with a solution, thought it may not be as clean as you hoped.
This thread mentioned a way to get the GtkAppChooser to "show all", but it doesn't actually show all applications you have installed. However from that I was able to work out how the GtkAppChooser is using Gio.AppInfo, which has Gio.AppInfo.get_all() (this is for PyObject) which returns a full list of Gio.AppInfos for all applications I have installed, provided they have a .desktop file.
So, my "solution" is to write my own app chooser which gets a list of apps from Gio.AppInfo.get_all().
I've cleaned up my previous solution to this and written a class 'AllAppChooser' to inherrit Gtk.Dialog, giving much greater customisation. The completed dialog looks like so:
And the code used:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
class AllAppChooser(Gtk.Dialog):
"""Provide a dialog to select an app from all those installed.
The regular Gtk.AppChooserDialog does not seem to provide any way to allow
selection from all installed apps, so this dialog serves as a replacement.
"""
def __init__(self, parent=None):
super().__init__(self)
self.set_default_size(350, 400)
self.set_icon_name('gtk-search')
self.set_title('App Chooser')
if parent:
self.set_parent(parent)
self.content_box = self.get_content_area()
self.content_box.set_margin_left(8)
self.content_box.set_margin_right(8)
self.content_box.set_margin_top(8)
self.content_box.set_margin_bottom(8)
self.content_box.set_spacing(8)
self.button_box = self.get_action_area()
self.button_box.set_margin_left(4)
self.button_box.set_margin_right(4)
self.button_box.set_margin_top(4)
self.button_box.set_margin_bottom(4)
self.label = Gtk.Label('Choose An Application')
self.content_box.pack_start(self.label, False, False, 0)
self.list_store = Gtk.ListStore(str, str, int)
pixbuf_renderer = Gtk.CellRendererPixbuf()
text_renderer = Gtk.CellRendererText()
icon_column = Gtk.TreeViewColumn('icon', pixbuf_renderer, icon_name=1)
text_column = Gtk.TreeViewColumn('text', text_renderer, text=0)
self.tree_view = Gtk.TreeView()
self.tree_view.set_model(self.list_store)
self.tree_view.set_headers_visible(False)
self.tree_view.append_column(icon_column)
self.tree_view.append_column(text_column)
self.view_port = Gtk.Viewport()
self.view_port.add(self.tree_view)
self.scroll_window = Gtk.ScrolledWindow()
self.scroll_window.add(self.view_port)
self.content_box.pack_start(self.scroll_window, True, True, 0)
self.ok_button = self.add_button(Gtk.STOCK_OK, 1)
self.ok_button.connect('clicked', self.on_ok)
self.cancel_button = self.add_button(Gtk.STOCK_CANCEL, 0)
self.selected_app = None
self.app_list = []
def populate_app_list(self):
"""Populate the list of apps with all installed apps.
Icons are provided by icon-name, however some apps may return a full
path to a custom icon rather than a themed-icon name, or even no name
at all. In these cases the generic 'gtk-missing-icon' icon is used.
"""
self.app_list = Gio.AppInfo.get_all()
for i in range(len(self.app_list)):
gio_icon = self.app_list[i].get_icon()
app_icon = 'gtk-missing-icon'
if gio_icon:
app_icon = gio_icon.to_string()
app_name = self.app_list[i].get_display_name()
self.list_store.append([app_name, app_icon, i])
self.list_store.set_sort_column_id(0, Gtk.SortType.ASCENDING)
def run(self):
"""Run the dialog to get a selected app."""
self.populate_app_list()
self.show_all()
super().run()
self.destroy()
return self.selected_app
def set_label(self, text):
"""Set the label text, \"Choose An App\" by default."""
self.label.set_text(text)
def on_ok(self, button):
"""Get Gio.AppInfo of selected app when user presses OK."""
selection = self.tree_view.get_selection()
tree_model, tree_iter = selection.get_selected()
app_index = tree_model.get_value(tree_iter, 2)
self.selected_app = self.app_list[app_index]
This is then run similar to a regular dialog:
app_chooser = AllAppChooser()
application = app_chooser.run()
If the user exits the dialog box or presses cancel then the result will be None, but if they selected an application then run() will return a Gio.AppInfo object for the application which you can then do with as you please. For example, to launch your newly selected application:
application.launch()
I feel this is now a relatively solid solution, but I still welcome additional suggestions. Additionally, if there is a way to do this in Gtk without having to do all this stuff I would still love to hear it.