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)
Related
I am making a simple application with 1 button and 1 label where pressing the button changes the text on the label. Both label and button and placed using the tkinter's grid system however when I press the button the label's text changes size as expected but the button becomes stretched to the label's length too. Why?
This is my code.
import tkinter as tk
def change():
label1.config(text = "example text that is really big which shows the button stretching")
window = tk.Tk()
window.config(bg="black")
b1=tk.Button(window,text="button1",font=("Segoe UI",40),command=change).grid(row=1,column=1,sticky="news")
label1=tk.Label(text = "text",font=("Segoe UI",20),bg="black",fg="white")
label1.grid(row=2,column=1,sticky="news")
window.mainloop()
The expected result was the label changing and the button's size staying the same but instead the button becomes stretched to the labels length. I have tried a lot of things trying to make this work but I can't figure it out.
Before pressing the button
After pressing the button
Setting sticky="news" for the button, will expand the button to fill the available space in the four directions North, East, West, South. Try changing it to sticky="w" to make it stick to the West. So, the line of code for creating the button will become:
b1=tk.Button(window,text="button1",font=("Segoe UI",40),command=change).grid(row=1,column=1,sticky="w")
is in tkinter any option, if i click on a button, the button color stays on the activebackground, i do my things on the appearing frame, and if i click next button, the color on the button (pressed before) dissappear and the next button gets the nnew color. i tried different bindfunctions. but <Enter>, <Leave> and <Button-1>, dont give me this effect. And is there any option, to combine it about two frames? I have in one fram icons and in the other texts (icons and text are buttons)
for example: i click (this is with activebackground)
#JacksonPro
self.home_btn = Button(self.navi_frame, bg="#181b20", image=home, bd=0, activebackground="#d06276")
self.home_btn.place(x=0, y=110, width=80, height=48)
self.home_btn.bind("<Enter>", self.home_moE)
self.home_btn.bind("<Leave>", self.home_moL)
def home_moE(self, event):
self.home_label["bg"] = "#d06276"
self.home_btn["bg"] = "#d06276"
but i want that this color stays, since i press another button.
I'm designing a GUI application using Tkinter and for this project, I need buttons for the menu. While looking into the buttons I wasn't blown away by the customization options that come with the buttons, especially when I found out that you can bind click arguments to rectangles.
This allows me to customize the "button" in (almost) limitless ways, but to allow me to put text on the button I need to create a rectangle element and a text element and bind them together using Tkinter's tag_bind property.
One of the design properties of the button that I wanted was active fill when the user moused over the element. Right now I'm just using activefill="" which works, except the text element and the button element will only fill while the mouse is over that element. So, for example, when I mouse over the button the button excluding the text will highlight and vise versa when I mouse over the text.
Below is a simplified (for brevity) version of what I use to generate the buttons;
button = canvas.create_rectangle(button_width, button_height, 10, 10, fill="000", activefill="111", tags="test")
text = canvas.create_text((button_width/2), (button_height/2), activefill="111", tags="test")
canvas.tag_bind("test", "<Button-1>", "foo")
Is there a way to bind the active fill function to a tag rather than a specific element?
Another option is that I completely missed a bunch of information about customizing the buttons in Tkinter, and I would not be apposed to learning about that.
Option 1
I would personally not go for the presented solution. I do not know if you are using the button provided by tk or ttk. But, with the tkinter.tk, you could absolutely change the appearance of the button.
Following, I give you an example that produces a button with the following characteristics:
Blue foreground
Flat appearance
When hovered, the background is green
When pressed, the background is red
The code is as follows:
import tkinter as tk
root = tk.Tk()
# Function hovering
def on_enter(e):
btn['background'] = 'green'
def on_leave(e):
btn['background'] = 'white'
# Create the button
btn = tk.Button(root, background='white', activebackground='red', foreground='blue',relief='flat',text='Test',width=20)
btn.pack()
# Bindings
btn.bind("<Enter>", on_enter)
btn.bind("<Leave>", on_leave)
# Loop
root.mainloop()
Option 2
If even after having tried the tk.Button, you are not glad with the result, I would create a Frame containing a Label (you can do nearly anything with that combination). Then, you could change the background of the frame according to any user action, like:
import tkinter as tk
root = tk.Tk()
# Function hovering
def on_enter(e):
lab['background'] = 'green'
def on_leave(e):
lab['background'] = 'white'
# Click
def on_click(e):
print("hi")
# Create the frame with a label inside
fr = tk.Frame(root)
lab = tk.Label(fr, text="Test", width=20, background="white")
# Packing
fr.pack()
lab.pack()
# Bindings
fr.bind("<Enter>", on_enter)
fr.bind("<Leave>", on_leave)
lab.bind("<Button-1>", on_click)
# Loop
root.mainloop()
You could even create a class with the above combination.
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')
The tkinter's Scrollbar associated with a Text widget doesn't always appear correctly when the ttk.Notebook tab that contains them is selected.
Here are two different procedures to demonstrate the strange behavior:
Run the program. Notice the 1st tab. Press the button, and lines are written to the Text widget in the second tab. Go to the 2nd tab. Note the scroll bar and the scroll button w/i the scroll bar is visible.
Run the program a 2nd time, but this time, before pressing the button, select the 2nd tab, do nothing, then select the 1st tab again. Then press the button, then select the 2nd tab. The Scroll button in the scroll bar is not visible. You must put your mouse in the text widget and scroll the mouse button to see it appear.
How can I fix this?
Here's my code:
from Tkinter import *
import ttk
class Log:
def __init__(self,nb,name):
# Make a frame and put it on a tab
self.frame= Frame(nb);
nb.add(self.frame, text=name)
# Put a scroll text on the frame
self.scrollBar=Scrollbar(self.frame)
self.text = Text(self.frame,
yscrollcommand=self.scrollBar.set,
width =40,
height=10)
self.scrollBar.config(command=self.text.yview)
self.text.pack(side=LEFT)
self.scrollBar.pack(side=RIGHT,fill=Y)
def fillLog(self):
for i in range(100):
self.text.insert('end','hellow world\n')
root = Tk()
nb = ttk.Notebook(root)
nb.pack()
log1 = Log(nb,'log1')
log2 = Log(nb,'log2')
ttk.Button(root, text='PUT TEXT IN 2nd tab',command=log2.fillLog).pack()
mainloop()
I am using Python 2.7 on Windows 7 (three computers).