Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a vertical-oriented button in tkinter?

Is it possible to orient a tk.Button or ttk.Button vertically? Smething like orienting a tk.Scrollbar in a way self.scrlbr = tk.Scrollbar(master, orient = vertical)?

Have tried tk.Button(*args).pack(fill = tk.Y), but it does not provide desired effect - button still gets horizontally oriented.

Found nothing in man pages, but maybe there is some not-staightforward way?

like image 842
Mikhail T. Avatar asked Oct 19 '22 06:10

Mikhail T.


2 Answers

The button widget does not provide this option but you can emulate the button widget to get the effect if required. One way, as mentioned is to use an image containing the rotated text. Depending on your theme you may also create a button using a canvas which allows you to rotate text drawn on itself using the angle option. This would look odd on Windows themes but could look normal where the widgets being used are Tk (and not ttk widgets) or with ttk provided the theme is one that uses Tk drawn elements (the default on unix).

A crude demo of how it would look:

enter image description here

import tkinter as tk
import tkinter.font as tkfont
main = tk.Tk()
font = tkfont.nametofont("TkDefaultFont")
label = "Click Me"
height = font.measure(label) + 4
width = font.metrics()['linespace'] + 4
canvas = tk.Canvas(main, height=height, width=width, background="SystemButtonFace", borderwidth=2, relief="raised")
canvas.create_text((4, 4), angle="90", anchor="ne", text=label, fill="SystemButtonText", font=font)
canvas.bind("<ButtonPress-1>", lambda ev: ev.widget.configure(relief="sunken"))
canvas.bind("<ButtonRelease-1>", lambda ev: ev.widget.configure(relief="raised"))
canvas.place(x=5, y=height + 10)
main.mainloop()
like image 130
patthoyts Avatar answered Oct 21 '22 05:10

patthoyts


I found an answer that is a bit easier. for example lets say you have 4 horizontal ttk.buttons, and you wish to place a vertical ttk.button next to them lets call our new button config. Using the grid method for the buttons, the first vertical button is at col=0, row=0 second at col=0, row=1 etc

Create a button with with=4 text=C\no\nn\nf\ni\ng\n, grid at col=1, row=0, rowspan=4,

There you have it. Simple and easy using standard tkinter.

like image 41
Peter Hedlund Avatar answered Oct 21 '22 05:10

Peter Hedlund