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

如何在Python Tkinter中将画布的宽度设置为屏幕的宽度?,python,canvas,tkinter,screen,Python,Canvas,Tkinter,Screen,我在tkinter中有一块画布,需要覆盖整个屏幕宽度,但无法让它工作 我试图将屏幕宽度设置为一个变量,并将其作为类中的一个参数传递,但它向我抛出了一个错误,我试图将root.winfo_screenwidth设置为self.winfo_screenwidth但它向我抛出了一个错误,我在www上查找答案,但我看到的最接近的事情是如何将根目录设置为屏幕大小 这是我的密码: import tkinter as tk class Window(tk.Frame): def __init__(

我在
tkinter
中有一块画布,需要覆盖整个屏幕宽度,但无法让它工作

我试图将屏幕宽度设置为一个变量,并将其作为类中的一个参数传递,但它向我抛出了一个错误,我试图将
root.winfo_screenwidth
设置为
self.winfo_screenwidth
但它向我抛出了一个错误,我在www上查找答案,但我看到的最接近的事情是如何将
根目录设置为屏幕大小

这是我的密码:

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

有人知道我如何将
画布
宽度设置为
屏幕宽度

您不需要显式设置画布的宽度
pack
提供了一些选项,您可以使用这些选项强制画布占据整个屏幕宽度

canvas.pack(fill="x")

如何为
“x”
“y”
执行此操作?@AdamDoesCoding:
fill=“两者”
。这只会将
“y”
扩展到我屏幕的3/4(
“x”
会扩展整个长度)。@AdamDoesCoding:您可能还需要包括
expand=True
,尽管很难说没有看到实际的代码。所有这些都有文档记录,在互联网上使用
pack
的例子很多。