python - 为什么我不能在 Python Ktinker 上移动我的下拉列表
问题描述
我希望将下拉菜单移至左上角。但是当我尝试 padx 和 pady 时,什么都没有发生。
global text
self.text = tk.Text(root, bg='black', foreground="white", height="15")
self.text.pack(padx=0, pady=75)
self.text.delete('1.0', tk.END)
self.text.insert(tk.END, "Opps, not yet connect OR no files to be read....")
self.variable = StringVar(root)
self.variable.set("Temperature") # default value
self.w = OptionMenu(root, self.variable, "Temperature", "Mazda", "three")
self.w.pack()
解决方案
由于默认元素居中,您需要anchor='w'
将元素向左移动 ( west
)
import tkinter as tk
root = tk.Tk()
txt = tk.Text(root, bg='black')
txt.pack(pady=75)
om_var = tk.StringVar(root, value='Hello')
om = tk.OptionMenu(root, om_var, 'Hello', 'World')
om.pack(anchor='nw') # north, west - top, left
root.mainloop()
或者您可以使用fill="x"
(或fill="both"
) 将大小调整为全宽
om.pack(fill='x')
但是还有其他问题-在Text
您使用的pady=75
情况下,您在 and 之间创建了边距Text
,OptionMenu
并且您需要pady=(75,0)
删除下面的边距Text
txt.pack(pady=(75,0))
import tkinter as tk
root = tk.Tk()
txt = tk.Text(root, bg='black')
txt.pack(pady=(75,0))
om_var = tk.StringVar(root, value='Hello')
om = tk.OptionMenu(root, om_var, 'Hello', 'World')
om.pack(fill='x')
root.mainloop()
推荐阅读
- javascript - 创建一个去抖函数,在搜索时延迟调用 func
- kubernetes - Kubernetes 自动扩展的可能指标在哪里定义
- hibernate - 即使使用 jandex 插件和空的 META-INF/beans.xml,Quarkus 也无法找到 Jandex 索引的第三方类
- ruby-on-rails - 在 TS 模块中使用全局 Rails-UJS(带有 webpacker 的 Rails)
- ios - 如何使用 Swift 4/5 扩展多个类
- c# - C# 控制台硒 | 从文本文件中读取行
- javascript - TypeError:无法在“Node”上执行“insertBefore”:参数 2 不是“Node”类型。角材料
- python - 如何比较二维分布?
- apache-kafka - 更改 Kafka Streams 内部主题的复制因子会影响更改日志/重新分区主题名称中的数字吗?
- bazel - 如何在 Bazel 中包含上层参考?