python - 如何在 Python Tkinter 中将画布的宽度设置为屏幕的宽度?
问题描述
我有一个画布tkinter
需要跨越屏幕的整个宽度,但无法让它工作。
我试图使屏幕宽度成为一个变量并将其作为参数传递给类但向我抛出一个错误,我试图将其设置root.winfo_screenwidth
为self.winfo_screenwidth
但它只是向我抛出一个错误,我在 www 上寻找答案但我看到的最接近的是如何设置root
屏幕尺寸。
这是我的代码:
import tkinter as tk
class Window(tk.Frame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
canvas = tk.Canvas(self, width="", height=100) #How to set the width to the screen width?
canvas.config(bg="#505050")
canvas.pack()
def mainloop_scw():
root = tk.Tk()
root.title("Editor - Adventure")
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
root.geometry("%sx%s" %(screenwidth, screenheight))
root.config(bg="#303030")
app = Window(root)
app.config(bg="#303030")
app.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
root.mainloop()
if __name__ == "__main__":
mainloop_scw()
有谁知道我如何将canvas
宽度设置为screenwidth
?
解决方案
您不需要明确设置画布的宽度。pack
具有可用于强制画布占据整个屏幕宽度的选项。
canvas.pack(fill="x")
推荐阅读
- android - Android 内置样式
- php - Laravel 无法识别刀片模板中的视频链接。应该如何修复?
- python - Spyder调试器:蓝色箭头消失,断点没有考虑
- javascript - 创建动态引导切换按钮时出错
- reactjs - CORS 策略 Laravel 和 React 已阻止访问从源“http://localhost:3000”获取“http://localhost:8000/api/puppies”
- java - 在 Java 中将布尔值添加到 LinkedList
- nlog - NLog当重复过滤器,是否可以添加条件?
- mplab - MPLAB X IDE 模拟器文档
- crystal-reports - Crystal Reports – 导出时的额外列(仅限 Microsoft Excel (97-2003) 格式)
- c# - .NET 5:未找到导入或有错误