简体   繁体   中英

Simple frame with tk in python

I'm trying to write a code that prints a Frame to the screen with a Button and Canvas in it

import tkinter as tk
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()
        self.text =tk.Text(height=20,width=10)
        self.text.pack()
        self.canvas=tk.Canvas(fill='Black')
        self.canvas.pack()
    def on_button(self):
        print(self.entry.get())
app = SampleApp()
app.mainloop()

As soon as I run it, I get an error:

_tkinter.TclError: unknown option "-fill"

I have no idea why.

填充是一个create_rectangle参数,而不是构造函数参数:

self.canvas.create_rectangle(0, 0, width, height, fill = "black") 

The canvas doesn't use a fill option to define what the background is; the error is coming out of the lower levels of the Tkinter code where it flips to the underlying Tcl/Tk runtime; option names get a hyphen put in front of them, and the error otherwise means what it says, “don't know what fill is in this context” (paraphrased).

However, the canvas does use a background option that takes a colour. Try:

    self.canvas=tk.Canvas(background='Black')

You can also create rectangles on the canvas; those are fillable. The overall canvas isn't a rectangle, it's a widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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