如何将 canvas 的宽度设置为 Python Tkinter 中屏幕的宽度?
How can I set the width of a canvas to the width of the screen in Python Tkinter?
我在 tkinter
中有一个 canvas 需要跨越整个屏幕宽度,但无法正常工作。
我尝试将屏幕宽度设置为一个变量并将其作为参数传递给 class 但向我抛出错误,我尝试将 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
?
您不需要显式设置 canvas 的宽度。 pack
具有可用于强制 canvas 占据整个屏幕宽度的选项。
canvas.pack(fill="x")
我在 tkinter
中有一个 canvas 需要跨越整个屏幕宽度,但无法正常工作。
我尝试将屏幕宽度设置为一个变量并将其作为参数传递给 class 但向我抛出错误,我尝试将 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
?
您不需要显式设置 canvas 的宽度。 pack
具有可用于强制 canvas 占据整个屏幕宽度的选项。
canvas.pack(fill="x")