Kivy scrollbar scroll direction with mouse - python

Is there a way to change the scroll behavior of Kivy scrollbars when using a mouse? With a mousewheel, the contents of a DropDown or Spinner scroll up or down as expected. However, if you use a mouse to grab the scrollbar and slide it up, the direction is reversed - you have to drag the mouse pointer down to move the scrollbar and list up.

This can be fixed by modifying the DropDown from which Spinner inherits to change scroll_type to include 'bars' (just 'content' by default). I fixed this behaviour as follows:
from functools import partial
dropdownMod = partial(DropDown, bar_width = 10, scroll_type = ['bars','content'])
class SpinnerName(Spinner):
dropdown_cls = dropdownMod
values = ('1','2','3')

Related

Tkinter - Getting x,y coordinates of Label object

I have defined a Label object as:
panel = Label(image_frame, image=self.img, cursor="cross")
Now, I'd like to do draw polygon on top of this, and I have created a function called draw() that binds to a canvas and will allow me to draw a polygon on top of it. So, I know my draw() command works.
However, I need to do this on top of the panel that I've defined as a Label.
The biggest problem I'm having is this line in my draw() command
if event.widget.canvasx(event.x)-2 < orig_x < event.widget.canvasx(event.x)+2 and event.widget.canvasy(event.y)-2 < orig_y < event.widget.canvasy(event.y)+2 :
I'm producing the following error:
AttributeError: 'Label' object has no attribute 'canvasx'
Is there an analog for canvasx for Label object? How can I bypass this without changing Label? Or is changing Label to canvas my only option?
The only other thing I can think of is to have a transparent canvas behind the Label, but then on-resize, things get messy.
Is there an analog for canvasx for Label object? How can I bypass this without changing Label? Or is changing Label to canvas my only option?
No, because there is no need. canvasx exists because the canvas can be scrolled in any direction, and you need to be able to convert from widget coordinates to the inner canvas coordinates.
In the case of a label, if you click at the 0,0 coordinate of a label, that will always be the upper left corner of the label, because the interior of the label cannot be scrolled.
Note: it is not possible to embed a label widget (or any other widget) on a canvas, and then draw on top of the label. The official canvas documentation says this about that:
Note: due to restrictions in the ways that windows are managed, it is not possible to draw other graphical items (such as lines and images) on top of window items. A window item always obscures any graphics that overlap it, regardless of their order in the display list.

How to position a ScrolledWindow (tkinter) scrollbar at the bottom

I am creating a gui with tkinter in python. I have created a scrollbar and this is what the section of code looks like:
beta_frame = Frame(width="500", height="680")
beta_frame.pack()
holder = ScrolledWindow(beta_frame, width=500, height=680)
holder.pack()
alpha_frame = holder.window
I would like to position this scrollbar at the very bottom every time something new is put on the screen (which would obviously be added to the bottom The only things I'm adding to the screen are labels and buttons), though I'm unsure how to do this and I've searched everywhere. All I came up with is the method see, which I am unsure if it is even applicable in this instance. Any help would be appreciated.
.see() is the normal way to get Tkinter to auto-scroll to a given position, but that method only exists on the widgets that have built-in support for scrolling - Listbox, Canvas, Text, and Entry. The Tix ScrolledWindow makes an ordinary Frame scrollable, so no such method will exist.
It appears that this line of code will do what you want:
holder.tk.eval(holder.vsb['command'] + " moveto 1.0")
vsb is the vertical scrollbar component of the ScrolledWindow, 'command' is the scrollbar configuration option that specifies a callback to invoke when the position is changed. This will refer to something deep inside Tix, but we don't care exactly what it is; we just invoke it with the same parameters that the scrollbar itself would, if being moved to the very end.

pyqt: Fixed Position

How can I give a widget a fixed position? Like so I can "attach"/put it at the bottom of the window and it will always be there; even when the window is expanded. I couldn't find anything useful on how to do it and, I suppose obviously, none of the obvious things work (resize(), setGeometry(), etc.). Any help?
I assume by "fixed position" you mean a position relative to one of the window edges. That's what your second sentence implies. So that's the question I will answer.
Use a layout manager with stretches and spacings. Here's a simple example to attach a widget "w" to the bottom of a window "win". This code typically gets called by (or goes inside) your window's constructor.
lay = QVBoxLayout(win)
lay.addStretch(1)
lay.addWidget(w)
The BoxLayout makes "w" stick to the bottom of the window and stay in that position as the window is resized.
you must reimplement the parent windows resizeEvent function, here is the code to make a widget "attached" to the bottom of the window:
def resizeEvent(self, event):
#widget.move(x, y)
self.bottom_widget.move(0, self.height() - self.bottom_widget.height())
#if you want the widgets width equal to window width:
self.bottom_widget.setWidth(self.width())
whenever the window is resized, this function will be called and it will move the widget to the bottom of the window. this is an absolute positioning approach, but you can always use QSpacerItem to push your widget to the bottom.

PyQt4 Toolbar Button Alignment

I have this code
Menu = self.menuBar()
EditMenu = Menu.addMenu("&File")
OptionMenu = Menu.addMenu("&Options")
HelpMenu = Menu.addMenu("&Help")
EditMenu.addActions((fileNewAction,faultAction,storeAction,localAction,scheduleAction))
OptionMenu.addAction(settingAction)
Toolbar = QtGui.QToolBar()
Toolbar.setIconSize(QtCore.QSize(50,50))
Toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon|QtCore.Qt.AlignLeading) #<= Toolbuttonstyle
self.addToolBar(QtCore.Qt.LeftToolBarArea,Toolbar)
Toolbar.addActions((fileNewAction,faultAction,scheduleAction,storeAction,localAction,settingAction))
settings = QtCore.QSettings()
self.restoreGeometry(settings.value("Geometry").toByteArray())
which give me this
i used
Toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon|QtCore.Qt.AlignLeading)
to display the text on the right side of the toolbar button and to align all the toolbar button images to the left. But the texts are not appearing on the right side.
If i remove the QtCore.Qt.AlignLeading,
I get unaligned (left side) buttons like this
(1) How do i get my toolbar button icons align to the left and display the text on the right side at the same time?
Another question is
(2) How do i adjust the width of the raised button effect when i mouse over on each button? I want the width of all the buttons to be the same. The width seems to be different depending on how long the text is.
IMHO, the problem is the QToolBar decides on a size of each button individually, disregarding neighboring buttons completely. But you can set the size manually:
for action in my_toolbar.actions():
widget = my_toolbar.widgetForAction(action)
widget.setFixedSize(width, height)

Adding Content to pyqt4 scroll area

How can I add content to a scroll area in pyqt4? DO i custom define a widget? For example,
if i had a array or a lista =[10,2,2,2,22,3,3,3]. How should I display teh variable in the scrollbar area?
If you want to add content to a scroll area, you need to define a new widget and add that to the scroll area - like you would add a widget to a frame. For example:
textEdit = QtGui.QTextEdit()
scrollArea = QtGui.QScrollArea(MainWindow)
scrollArea.setWidget(textEdit)
Then, you can use textEdit.append() or textEdit.setText() to add the data in the array to the text box in the scroll area. The documentation says it all, really, albeit in C rather than python, but its obvious what you need to do:
QLabel *imageLabel = new QLabel;
QImage image("happyguy.png");
imageLabel->setPixmap(QPixmap.fromImage(image));
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette.Dark);
scrollArea->setWidget(imageLabel);

Categories