【问题标题】:How can I set the width of a canvas to the width of the screen in Python Tkinter?如何在 Python Tkinter 中将画布的宽度设置为屏幕的宽度?
【发布时间】:2019-09-01 09:43:00
【问题描述】:

我在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

【问题讨论】:

    标签: python canvas tkinter screen


    【解决方案1】:

    您不需要明确设置画布的宽度。 pack 具有可用于强制画布占据整个屏幕宽度的选项。

    canvas.pack(fill="x")
    

    【讨论】:

    • 我该如何为"x""y" 执行此操作?
    • @AdamDoesCoding: fill="both".
    • 这只会将"y" 扩展到我屏幕的3/4("x" 确实会扩展整个长度)。
    • @AdamDoesCoding:您可能还需要包含expand=True,尽管如果没有看到实际代码很难说。所有这些都有记录,互联网上有很多使用pack 的示例。
    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2020-08-30
    相关资源
    最近更新 更多