简体   繁体   English

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

[英]How can I set the width of a canvas to the width of the screen in Python Tkinter?

I have a canvas in tkinter that needs to span the whole width of the screen, but cannot get it to work.我在tkinter中有一个需要跨越整个屏幕宽度的画布,但无法让它工作。

I tried to make the screen width a variable and pass it along as a parameter in the class but throws a error at me, I tried to set the root.winfo_screenwidth to self.winfo_screenwidth but it just throws a error at me, I looked on the www for answers but the closest thing I saw was how to set the root to the screen size.我试图使屏幕宽度成为一个变量并将其作为类中的参数传递但向我抛出一个错误,我试图将root.winfo_screenwidth设置为self.winfo_screenwidth但它只是向我抛出一个错误,我看着www 的答案,但我看到的最接近的是如何将root设置为屏幕大小。

Here is my code:这是我的代码:

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

Does anyone know how I can set the canvas width to screenwidth ?有谁知道如何将canvas宽度设置为screenwidth

You don't need to explicitly set the width of the canvas.您不需要显式设置画布的宽度。 pack has options that you can use to force the canvas to occupy the full width of the screen. pack具有可用于强制画布占据整个屏幕宽度的选项。

canvas.pack(fill="x")

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

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