5

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
Mikhail T.
  • 1,240
  • 12
  • 21
  • Only thing I can come up with is organising `tk.Button(master, text = 'a\nb\nc\n\d\n\e').pack(fill = tk.Y)`. It gets Button vertical orientation, but may be there is some more robust way? Thanks again – Mikhail T. Jun 24 '16 at 07:58
  • 1
    Do you actually want the text in the button oriented vertically, i.e. rotated by 90° and running from top to bottom (or vice versa)? Maybe you could add a picture. – tobias_k Jun 24 '16 at 11:50
  • Thank you! Having text turned by 90 degrees would be great as well. But yes, [this answer](http://stackoverflow.com/a/38012733/6470235) with using a picture is only availble solution I know at the moment. – Mikhail T. Jun 24 '16 at 11:57

2 Answers2

3

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()
patthoyts
  • 32,320
  • 3
  • 62
  • 93
1

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.

  • This is simple and gets the work done. Thus a worthy idea. In summary, create the button with a narrow width (3 is fine for me) and write its text with each letter followed by a linefeed character (\n), as in my_b=ttk.Button(a_frame,text='M\nY\nT\nE\nX\nT',width=3,command=jj) – JC Cheloven Aug 30 '22 at 10:28