繁体   English   中英

如何在 Python Tkinter 中将画布的宽度设置为屏幕的宽度?

[英]How can I set the width of a canvas to the width of the screen in 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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM