PyQt: Can I make a toolbar button with an icon and text? - python

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

How to set QAction icon text [duplicate]

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")

How to make Text scroll down automatically whenever text overcomes the visual area?

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)

Remove window title bar in TKinter Python

How do I remove the title bar from a Toplevel() window in Tkinter.
Right now I for my main I have
self.master.title("Subtest")
self.master.geometry("400x200")
self.alertwindow()
Label(self.master,textvariable=self.connected,height=4).grid(row=0,column=0)
Button(self.master,text="Monitor",command= lambda: self.startnewthread(1),width=10).grid(row=6,column=1)
Button(self.master,text="Quit",command=self.haltprogram).grid(row=6,column=0)
And for my alert window function I have
def alertwindow(self):
self.listbox=Listbox(Toplevel(self.master,width=150).overrideredirect(True),width=150).pack)
I was wanting the program to open up a root window, and then a toplevel listbox without a title bar; however, the only thing the program is doing right now is freezing, and when I remove the .overrideredirect(True), the program launches two listbox windows. How can I have the program open only one listbox without a title bar on windows? Thanks
Looking at this line
self.listbox=Listbox(Toplevel(self.master,width=150).overrideredirect(True),width=150).pack)
It's pretty clear you're trying to do WAY too much on 1 line. (Your parenthesis don't even match). Let's break it up, shall we?
new_top = Toplevel(self.master,width=150)
new_top.overrideredirect(True)
self.listbox = Listbox(new_top,width=150)
self.listbox.pack()
Also note that you seem to be using .grid and .pack -- Generally that's ill advised and Tkinter will happily spend all of eternity trying to negotiate a proper placement of a widget when you try to use them together.
My guess about what's happening:
your actual code has properly balanced parenthesis so there is no SyntaxError
Toplevel.overrideredirct returns None
Listbox sees None as the parent widget and substitutes the root widget (Tk)
Then you're using .grid and .pack both on the root widget which causes your program to hang.

Determining what tkinter window is currently on top

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.

How to get (childless) “tabs” in a pygtk application

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 !

Categories