在tkinter中可以有一个垂直方向的按钮吗? [英] Is it possible to have a vertical-oriented button in tkinter?

查看:48
本文介绍了在tkinter中可以有一个垂直方向的按钮吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以垂直放置 tk.Button ttk.Button ?类似于以 self.scrlbr = tk.Scrollbar(master,orient = vertical)?

的方式定向 tk.Scrollbar ?

尝试过 tk.Button(* args).pack(fill = tk.Y),但是它不能提供理想的效果-按钮仍会水平放置.

在手册页中什么都没找到,但是也许有一些不为人所知的方法吗?

解决方案

按钮小部件不提供此选项,但是如果需要,您可以模拟按钮小部件以获得效果.如上所述,一种方法是使用包含旋转文本的图像.根据您的主题,您还可以使用 canvas 创建一个按钮,该按钮允许您使用 angle 选项旋转在其自身上绘制的文本.在Windows主题上,这看起来很奇怪,但是在使用的小部件是Tk(而不是ttk小部件)或使用ttk的情况下,如果主题是使用Tk绘制元素的元素(unix的默认值),则看起来很正常.

关于它的外观的原始演示:

 将tkinter导入为tk将tkinter.font导入为tkfont主= tk.Tk()font = tkfont.nametofont("TkDefaultFont")标签=点击我"高度= font.measure(label)+ 4宽度= font.metrics()['linespace'] + 4canvas = 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() 

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?

解决方案

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:

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

这篇关于在tkinter中可以有一个垂直方向的按钮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆