I am learning PyQt by playing with examples. In this case, I'm playing with the webbrowser example that is located at \Python26\Lib\site-packages\PyQt4\examples\activeqt\webbrowser .
The demo does something really odd if you add one line to set the icon text property of a QAction.
Here's a code sample of the change I tried:
self.actionBack = QtGui.QAction(MainWindow)
self.actionBack.setIcon(QtGui.QIcon(":/icons/image1.xpm"))
self.actionBack.setObjectName("actionBack")
# added this line:
self.actionBack.setIconText("Back")
One time I tried it, and the entire toolbar went blank. I can't reproduce that, now I have no effect from that one line change.
What I'm trying to figure out is what to do to the QAction so that the toolbar has text on the button beside the image, or can it be done at all like this? Is there some other way to make the toolbar have some text plus an icon?
Try setting the button style on the toolbar that the actions are being added to:
self.tbNavigate.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
...
self.actionBack.setIconText("Back")
Related
I am using the IPython module in a Jupyter Notebook.
I am using the display module to display buttons.
from ipywidgets import widgets
import IPython.display as dsply
def click_reset(b):
print("reset domains button")
restoreDomains()
resetButton = widgets.Button(description="Reset Domains")
resetButton.on_click(click_reset)
dsply.display(resetButton)
This works fine, but I am trying to find a way to programatically hide certain buttons. Based off the execution of my other code, I want certain buttons to be removed from the UI. Is there anything like hide(resetButton) that I can use?
You can hide a widget using
resetButton.layout.visibility = 'hidden'
to let the widget still consume space, or
resetButton.layout.display = 'none'
to let the widget not consume space anymore.
The top-level attribute resetButton.visible = False is not longer supported.
When I use #SergeyGornostaev's answer, I still have a residual cross showing up on the left side of the cell output. I found the following command removes the widget all together:
resetButton.close()
You can hide every widget by setting it's property visible to False
resetButton.visible = False
Let's say I have a text widget called self.txt. I also have a scrollbar, called scroll.
I have configured the scrollbar to work with self.txt, but I need the Text widget to stay scrolled down, whenever text gets added to it.
Is this doable?
I think this should work: every time the text is modified, this should be called:
def modified(self, event):
self.txt.see(END) # tkinter.END if you use namespaces
To catch the modification, use:
self.txt.bind('<<Modified>>', self.modified)
I have written an application in python 2.7 and tkinter. I created a tool bar with several buttons that open up respective top windows that display various options. I used ttk.Checkbutton with the 'toolbutton' style as an indicator to show whether the option windows are open or closed.
The problem is that the option windows will go to the back if another window is selected. Currently, if one selects the toolbutton again, the option window will close. However, I only want to close the window if it is on top. If the option window is not on top, I want the window to moved to the front.
Some of the code I have working:
class MainWindow:
def __init__(self,application):
self.mainframe=tk.Frame(application)
application.geometry("900x600+30+30")
self.otherOptionsSelect=tk.IntVar()
self.otherOptions_Button=ttk.Checkbutton(application,style='Toolbutton',variable=self.otherOptionsSelect,
onvalue=1, offvalue=0,image=self.optionsIcon, command=self.otherOptions)
def otherOptions(self):
if self.otherOptionsSelect.get()==0:
self.otherOptions.destroy()
return
self.otherOptions=tk.Toplevel()
self.otherOptions.title("IsoSurface Options")
self.otherOptions.geometry("200x165+"+str(int(application.winfo_x())+555)+"+"+str(int(application.winfo_y())+230))
self.otherOptApply_button=ttk.Button(self.otherOptions,text="Apply",command=self.showFrame)
self.otherOptApply_button.place(x=20,y=80,width=50,height=30)
self.otherOptClose_button=ttk.Button(self.otherOptions,text="Close",command=self.otherOptionsClose)
self.otherOptClose_button.place(x=80,y=80,width=50,height=30)
def otherOptionsClose(self):
self.otherOptionsSelect.set(0)
self.otherOptions.destroy()
Here is a picture of the entire application I have written:
In the above image, each window has their respective ttk.checkbutton. At the moment, toggling the checkbutton either opens or closes the window. However, what I really want it to do is close the window if the window is in front of the application, or bring the window to the front if it is behind the application.
Hopefully this clears some things up.
Thanks in advance!
It is in fact possible to check stacking order of windows. Using Tkinter, you have to do some funny tcl evals to get at the information. I found the answer at TkDoc in the section on Windows and Dialogs, scroll down until you get to "Stacking Order". The code baffled me until I started playing around with it interactively. My test code was:
import Tkinter as tk
root = tk.Tk()
root.title('root')
one = tk.Toplevel(root)
one.title('one')
two = tk.Toplevel(root)
two.title('two')
I then manipulated the windows so that two was on top, one under that and root below them all. In that configuration, the following weirdness can tell you relative layering of windows:
root.tk.eval('wm stackorder '+str(two)+' isabove '+str(root))
returns 1, meaning "Yes, window two is above window root." While the following:
root.tk.eval('wm stackorder '+str(root)+' isabove '+str(two))
returns 0, meaning "No, window root is not above window two." You can also use the command:
root.tk.eval('wm stackorder '+str(root))
Which gives back the full window stacking order in the form of a weird string something like this:
'. .68400520L .68401032L'
Which starts to make sense when you run the commands:
str(root)
str(one)
str(two)
and figure out that root has the internal name '.', one is '.68400520L' and two is '.68401032L'. You read the output of root.tk.eval('wm stackorder '+str(root)) backwards so it's saying two is on top, one is under that and root is below both.
I am learning PyQt by playing with examples. In this case, I'm playing with the webbrowser example that is located at \Python26\Lib\site-packages\PyQt4\examples\activeqt\webbrowser .
The demo does something really odd if you add one line to set the icon text property of a QAction.
Here's a code sample of the change I tried:
self.actionBack = QtGui.QAction(MainWindow)
self.actionBack.setIcon(QtGui.QIcon(":/icons/image1.xpm"))
self.actionBack.setObjectName("actionBack")
# added this line:
self.actionBack.setIconText("Back")
One time I tried it, and the entire toolbar went blank. I can't reproduce that, now I have no effect from that one line change.
What I'm trying to figure out is what to do to the QAction so that the toolbar has text on the button beside the image, or can it be done at all like this? Is there some other way to make the toolbar have some text plus an icon?
Try setting the button style on the toolbar that the actions are being added to:
self.tbNavigate.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
...
self.actionBack.setIconText("Back")
I am facing the problem to need tabs in a pygtk app. Pretty much just like gedit has, but without any per-child widget content.
I’ve come across gtk.Notebook, but that requires me to put a widget for each tab, which I don't want.
The reason is, that I have one widget, but would only like to updates its content based on which tab is selected.
Any hints on how to do that?
My idea so far would be to just add some invisible widget for each tab and then connect to the select-page signal. Which widget could I use as invisible widget, or is there a better/alternative way of achieving my goal?
The invisble widget idea works. But not with gtk.Invisible (this just crashes), but with gtk.HBox() or any other thing that seems empty.
self.notebook.append_page(gtk.HBox(), gtk.Label("title"))
Now if I want to display stuff inside the tab actually, I can use reparent to move the widget to the current tab like this.
class Tab(gtk.HBox):
def __init__(self, child):
self.child = child
self.notebook.append_page(Tab(myWidget), gtk.Label("title"))
def pageSelected(self, notebook, page, pagenum):
box = notebook.get_nth_page(pagenum)
box.child.reparent(box)
You can have global widgets, one per tab as you want, in order to access them easily when the tab is selected.
self.notebook.append_page(self.rightBox, gtk.Label("Orders"))
Then connect to the "switch page" signal
self.notebook.connect("switch-page", self.pageSelected)
and :
def pageSelected(self, notebook, page, pagenum):
name = notebook.get_tab_label(notebook.get_nth_page(pagenum))
Now you have "name" with the label of the currently selected page. Just test it (if name == "Orders" ...) to interact.
Hope this was of some help !