I'm creating a tool bar in PyQt. How it looks now is:
(HomeButton).............................................(ExitButton)..|
I want to use the space in the middle to put in an image/logo -- with no function so it looks like:
(homebutton)......[IMAGE/LOGO_HERE]......(exitbutton)..|
I've tried to do this by adding a widget with an image but it's not showing up. My code is:
logo = QWidget()
logolabel = QLabel(p3logo)
logopixmap = QPixmap(self.LOGO)
logolabel.setPixmap(QPixmap(self.LOGO))
logolabel.setPixmap(logopixmap)
logo.resize(logopixmap.width(),logopixmap.height())
###logoAction = QAction(QIcon('logo.png'), 'Logo', self)
spacer = QWidget()
spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
exitAction = QAction(QIcon('exit.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+X')
exitAction.triggered.connect(self.exitClicked)
homeAction = QAction(QIcon('home.png'), 'Home', self)
homeAction.setShortcut('Ctrl+H')
homeAction.triggered.connect(self.homeClicked)
self.toolbar = self.addToolBar('Toolbar')
self.toolbar.addAction(homeAction)
self.toolbar.addWidget(logo)
###self.toolbar.addAction(logoAction)
self.toolbar.addWidget(spacer)
self.toolbar.addAction(exitAction)
self.toolbar.addSeparator()
I also tried to add it in as an 'icon' but it was resized to the same size as the home/exit buttons making it hardly visible.
Your code example is effectively just adding an empty widget to the toolbar, because the label has not been put inside a layout. It looks like it can be fixed by getting rid of the container widget and simply adding the label directly:
self.toolbar.addWidget(logolabel)
Related
I need to create a toolbar (on the left side for example) that will contain many buttons. On default if overall height of all buttons is greater than the hight of toolbar these surplus buttons will be hidden. And I want to make this toolbar show all buttons and allow me to scroll down to see the rest. I couldn't find anything usefull on the web so far. Any ideas?
You should be able to stick the QToolBar inside a QScrollArea.
toolbar = QtGui.QToolBar()
toolbar.setOrientation(QtCore.Qt.Vertical)
for i in range(20):
toolbar.addAction('Action{0}'.format(i))
scroll_area = QtGui.QScrollArea()
scroll_area.setWidget(toolbar)
For anyone interested here is the solution:
Thanks to #Brendan Abel's answer I've came up with an idea. What I did is I've created my toolbar the same way I did before. Then I've added all my widgets (that previously were in this toolbar) to the new QWidget with QVBoxLayout. Then I've created a QScrollArea and set my recently-created-widget as a child widget of this scroll area. And finally I've added my ScrollArea to the Toolbar using addWidget().
class LeftToolbar(QtGui.QToolBar):
def __init__(self, *args):
QToolBar.__init__(self, *args)
self.setFloatable(False)
self.setMovable(False)
self.scroll_widget = QtGui.QWidget(self)
self.scroll_layout = QtGui.QVBoxLayout()
self.scroll_widget.setLayout(self.scroll_layout)
# Add your toolbar widgets here
self.ExampleWidget1 = QtGui.QLabel(self)
self.ExampleWidget1.setText("Example Text1")
self.scroll_layout.addWidget(self.ExampleWidget1)
self.ExampleWidget2 = QtGui.QLabel(self)
self.ExampleWidget2.setText("Example Text2")
self.scroll_layout.addWidget(self.ExampleWidget2)
# Create QScrollArea
self.scroll_area = QtGui.QScrollArea()
self.scroll_area.setWidget(self.scroll_widget)
self.addWidget(self.scroll_area)
# Create object LeftToolbar in your main window
self.LeftToolbar = LeftToolbar()
self.addToolBar(Qt.LeftToolBarArea, self.LeftToolbar)
I have some code here that when an image (which is my button) is clicked, a new image randomly appears. This is due to a table I created with some images inside.
local animalPic
local button = display.newImageRect ("images/animalBtn.jpg", 200, 200)
button.x = 250
button.y = 50
local myPics = {"images/animal1.png", "images/animal2.png"}
function button:tap (event)
local idx = math.random(#myPics)
local img = myPics[idx]
local animalPic = display.newImage(img)
animalPic.x = contentCenterX
animalPic.y = contentCenterY
end
button:addEventListener ("tap", button)
The problem with it is the graphics just keep piling up when I click the button. The correct behavior should be -
Button is clicked and an image is shown while removing the previous image. How do I incorporate this behavior? I already tried the removeSelf command and it doesnt work......Any help appreciated.
You declare animalPic each time you enter function. You should declare it once and then remove it and replace it by another.
It should be:
local animalPic
function button:tap (event)
local idx = math.random(#myPics)
local img = myPics[idx]
animalPic:removeSelf()
animalPic = nil
animalPic = display.newImage(img)
animalPic.x = contentCenterX
animalPic.y = contentCenterY
end
When you call display.newImage(), you are adding a new image. The problem is that you need to remove/hide the original one. Perhaps you really need two objects - one for the current image and one for the tap event. When the tap event occurs, hide the old image and display the new one. An alternative would be to load all the images in their own imageRects and then toggle them on and off.
Hi i am trying to put an image as the background on one of my buttons, i have already done this on lots of other buttons in my main window but this particular button sits inside a top level window and the image doesn't load like it should, does anyone know why? (i have also tried defining the width and height of the button but that still doesn't show the image)
def rec_window():
recw = Toplevel(width=500,height=500)
recw.title('Record To.....')
img1 = PhotoImage(file="C:/Users/Josh Bailey/Desktop/pi_dmx/Gif/mainmenu.gif")
Button(recw, image=img1, command=rec_preset_1).grid(row=1, column=1)
Button(recw, text="Preset 2", bg = 'grey70',width=40, height=12,command=rec_preset_2).grid(row=1, column=2)
Button(recw, text="Preset 3", bg = 'grey70',width=40, height=12,command=rec_preset_3).grid(row=2, column=1)
Button(recw, text="Preset 4", bg = 'grey70',width=40, height=12,command=rec_preset_4).grid(row=2, column=2)
Button(recw, text="Cancel", bg='grey70', width=20, height=6, command=recw.destroy). grid(row=3,column=1,columnspan=2, pady=30)
Depending on how the rest of your program is structured, your image might be getting cleared by garbage-collection:
From http://effbot.org/tkinterbook/photoimage.htm
Note: When a PhotoImage object is garbage-collected by Python (e.g.
when you return from a function which stored an image in a local
variable), the image is cleared even if it’s being displayed by a
Tkinter widget.
To avoid this, the program must keep an extra reference to the image
object. A simple way to do this is to assign the image to a widget
attribute, like this:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
In your case, you can start your function by declaring img1 as a global variable to retain a reference:
global img1
or, if you already have img1 elsewhere in your program:
img1 = PhotoImage(file="C:/Users/Josh Bailey/Desktop/pi_dmx/Gif/mainmenu.gif")
img1Btn = Button(recw, image=img1, command=rec_preset_1)
img1Btn.image = img1
img1Btn.grid(row=1, column=1)
I work on a project using Gtk & Ruby. I want when one icon was selected, it shouldn't seem as a rectangle.
http://i.imgur.com/RIEBipq.png
It just seem icon bound was selected. How can supply this?
http://i.imgur.com/LzT4H3C.png
I use iconview like below:
iconview = Gtk::IconView.new
file_store = Gtk::ListStore.new(String, String, TrueClass, Gdk::Pixbuf)
iconview.model = file_store
iconview.text_column = COL_DISPLAY_NAME
iconview.pixbuf_column = COL_PIXBUF
How can I code for this appearance?
i am using ruby-gtk. There is a notebook in my application. I added textView to scrolledWindow. I want to get textView buffer. My some code
editor = Textview.new
swin = Gtk::ScrolledWindow.new
tab = Gtk::Notebook.new
swin.add(editor)
tab.append_page(swin, Gtk::Label.new("Tab")
tab.get_nth_page(current_page).buffer # wrong because its contain is a scrolledWindow
How can i get editor buffer?
As GtkScrolledWindow is a GtkBin, you can make use of child method to get the widget added as the child. In your case it should return GtkTextView. Try something on the lines of:
tab.get_nth_page(current_page).child.buffer
Hope this helps!