python - 如何创建一个框架,其中包含一定大小的“空”消息小部件
问题描述
我对这段代码的问题是,当您将鼠标悬停在按钮上时出现的文本框会调整整个窗口的大小。我想要的是这个框架与将信息放置在那里的文本字段具有相同的大小。
import tkinter
import re
from tkinter import *
from tkinter import ttk, filedialog
from ctypes import windll
from tkinter.scrolledtext import ScrolledText
class MainApp(Tk):
def __init__(self):
super().__init__()
#self.geometry("500x300")
self.title("My Bioinformatics Toolbox")
def fasta_button_hover(hover):
instructions_field.config(text="This function will allow the user to open, "
"read and see the contents of a .FASTA file. It "
"will also allow the user to save their own sequence "
"as a FASTA file")
def fasta_button_leave(hover):
instructions_field.config(text="")
# Create the menu bar
menu_bar = tkinter.Menu(self)
file_menu = tkinter.Menu(menu_bar, tearoff=0)
# The File cascade in the menu bar
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=self.destroy)
self.config(menu=menu_bar)
# Create the buttons frame
button_frame = ttk.Frame(self)
#button_frame.configure(height=button_frame["height"], width=button_frame["width"])
#button_frame.grid_propagate(0)
button_frame.pack(side=LEFT)
# Create the text field frame
text_frame = ttk.Frame(self, width=500)
text_frame.pack(side=RIGHT)
# Create the instructions field
instructions_field = Message(text_frame, width=300)
instructions_field.grid(column=0, row=0)
# Create the buttons
fasta_button = ttk.Button(button_frame, text="Read FASTA", width=12)
fasta_button.bind("<Enter>", fasta_button_hover)
fasta_button.bind("<Leave>", fasta_button_leave)
fasta_button.grid(column=0, row=0)
dna_to_rna_button = ttk.Button(button_frame, text="DNA to RNA", width=12)
dna_to_rna_button.grid(column=0, row=1)
example_button = ttk.Button(button_frame, text="Example", width=12)
example_button.grid(column=0, row=2)
example_button2 = ttk.Button(button_frame, text="Example2", width=12)
example_button2.grid(column=0, row=3)
example_button3 = ttk.Button(button_frame, text="Example3", width=12)
example_button3.grid(column=0, row=4)
# A separator line ("rowspan" is set to a safe number of rows)
button_separator = ttk.Separator(button_frame, orient="vertical")
button_separator.grid(column=1, row=0, rowspan=100, sticky="ns")
# Fix for windows DPI scaling
windll.shcore.SetProcessDpiAwareness(1)
app = MainApp()
app.mainloop()
我希望我的窗口始终拥有这种尺寸的全尺寸应用程序,但是当我不悬停时,它具有这种尺寸的小型应用程序
我的猜测是我错过了一些东西text_frame
,但我无法弄清楚......
编辑以更新缩进
解决方案
对您的代码的此修改似乎实现了这一点:
# Create the instructions field
instructions_field = ttk.Label(text_frame, width=50, wraplength=300)
instructions_field.grid(column=0, row=0)
所做的更改:
- 用小部件替换
Message
小Label
部件 - 在标签中设置
width
和关键字参数wraplength
- 手动调整换行长度(换行前的像素数)以适合小部件内的所有文本
它可以说不是最优雅的解决方案,但仍然是一个有效且可靠的解决方案
注意:Label 中的 width 关键字参数取为字符宽
推荐阅读
- php - 使用 PHP 和 MySQL 创建一个名为登录用户的用户名的文件夹
- unity3d - 跟随生成的 iPhone 播放器相机移动 Leap Motion 手坐标系
- swift - 无法将“Work.Login”(0x10c5edd20)类型的值转换为“Work.DataBaseConn”
- html - 当 CSS 网格中的元素使用多于一行/列时显示排水沟
- r - 使用 cbind 和其中一个或参数组合两个向量
- python - 如何从具有匹配索引的数据框中减去系列
- javascript - 无法处理承诺中的错误
- c++ - vptr 上的数据竞争是否明确非法?
- java - 启动画面不能在移动设备中工作,但在模拟器中工作
- spring - 如何使用有效负载变量在 Springboots 中构造 @service