Python代码作为模块而不是作为类

时间:2016-03-07 20:09:45

标签: python user-interface tkinter python-3.5 tkinter-canvas

我试图找出原因,但也许不知道正确的问题。我有一个项目,我正在尝试为我的汽车创建仪表板。问题是,当我尝试从另一个类创建一个canvas对象时,我的GUI启动但没有显示任何内容。如果我在没有要求画布显示的情况下启动GUI,则GUI工作并显示屏幕。我是Python的新手。我的版本是3.5

我的主要节目:

from tkinter import*
from helperThingys import Helpers

root = Tk()
root.geometry("800x480")

bgLeft = Frame(root, bg="black", width=200, height=480)
bgLeft.pack_configure(fill=BOTH, expand=1, side=LEFT)
bgMiddle = Frame(root, bg="black", width=400, height=480)
bgMiddle.pack_configure(fill=BOTH, expand=1, side=LEFT)
bgRight = Frame(root, bg="black", width=200, height=480)
bgRight.pack_configure(fill=BOTH, expand=1, side=LEFT)

# These are really gif images read to labels to be able to grid them
# now just plain text labels for convenience. 
label_4hi = Label(bgLeft, text="ok", bg="green")
label_4lo = Label(bgLeft, text="ok", bg="green")
label_lock = Label(bgLeft, text="ok", bg="green")
label_batt = Label(bgMiddle, text="ok", bg="red")
label_fuelF = Label(bgLeft, text="ok", bg="orange")
label_glow = Label(bgLeft, text="ok", bg="yellow")
label_hb = Label(bgMiddle, text="ok", bg="blue")
label_indL = Label(bgMiddle, text="ok", bg="green")
label_indR = Label(bgMiddle, text="ok", bg="green")
label_lowFuel = Label(bgLeft, text="ok", bg="red")
label_lowOil = Label(bgLeft, text="ok", bg="red")
label_park = Label(bgMiddle, text="ok", bg="red")
label_pwrS = Label(bgLeft, text="ok", bg="red")
label_rFog = Label(bgLeft, text="ok", bg="orange")
label_temp = Label(bgLeft, text="ok", bg="red")
label_gauge = Label(bgMiddle, text="ok", bg="red")

# Middle frame
bgMiddle.rowconfigure(0, minsize=46, weight=1)
bgMiddle.rowconfigure(1, minsize=217, weight=5)
bgMiddle.rowconfigure(2, minsize=217, weight=5)
bgMiddle.columnconfigure(0, minsize=80)
bgMiddle.columnconfigure(1, minsize=80)
bgMiddle.columnconfigure(2, minsize=80)
bgMiddle.columnconfigure(3, minsize=80)
bgMiddle.columnconfigure(4, minsize=80)

label_indL.grid(row=0, column=0, sticky=E+N+W)
label_hb.grid(row=0, column=1, sticky=E+N+S+W)
label_park.grid(row=0, column=2, sticky=E+N+S+W)
label_batt.grid(row=0, column=3, sticky=E+N+S+W)
label_indR.grid(row=0, column=4, sticky=E+N+S+W)

# Left frame
bgLeft.rowconfigure(0, minsize=46, weight=1)
bgLeft.rowconfigure(1, minsize=46, weight=1)
bgLeft.rowconfigure(2, minsize=46, weight=1)
bgLeft.rowconfigure(3, minsize=46, weight=1)
bgLeft.rowconfigure(4, minsize=46, weight=1)
bgLeft.columnconfigure(0, minsize=100)
bgLeft.columnconfigure(1, minsize=100)

label_4hi.grid(row=0, column=0, sticky=E+N+S+W)
label_4lo.grid(row=0, column=1, sticky=E+N+S+W)
label_lock.grid(row=1, column=0, sticky=E+N+S+W)
label_pwrS.grid(row=1, column=1, sticky=E+N+S+W)
label_rFog.grid(row=2, column=0, sticky=E+N+S+W)
label_lowFuel.grid(row=2, column=1, sticky=E+N+S+W)
label_lowOil.grid(row=3, column=0, sticky=E+N+S+W)
label_temp.grid(row=3, column=1, sticky=E+N+S+W)
label_fuelF.grid(row=4, column=0, sticky=E+N+S+W)
label_glow.grid(row=4, column=1, sticky=E+N+S+W)

# Middle frame
speedo = helpers.Gauge()
speedo.makeGauge(bgMiddle, 150, "speed")
speedo.grid(row=1, columnspan=5, sticky=E+N+S+W)
#label_gauge.grid(row=1, columnspan=5, sticky=E+N+S+W)

# Right Frame

#oilPres = Gauge(bgRight, 150, "oilPres")

root.mainloop()

这是来自helperThingys包的'helpers.py'。

from tkinter import Canvas

class Gauge(Canvas):

    def __init__(self):
        Canvas.__init__(self)
        pass

    def makeGauge(self, window, value, gaugeType):
        if gaugeType == "speed":
            baseCanvas = Canvas(window, bg="black", width=400, height=217,
                                highlightthickness=0)
            baseCanvas.create_arc([10, 5, 390, 395], start=0, extent=180,
                                  fill="white")
            return baseCanvas

我正在使用画布作为仪表的背景。奇怪的是,这个'helpers.py'工作并创建了画布到我的GUI,当它不是一个类而是一个模块时:

def makeGauge(self, window, value, gaugeType):
    if gaugeType == "speed":
        baseCanvas = Canvas(window, bg="black", width=400, height=217,
                                highlightthickness=0)
        baseCanvas.create_arc([10, 5, 390, 395], start=0, extent=180,
                                  fill="white")
        return baseCanvas

......等等(然后该模块与GUI在同一个包中)。这就是我知道画布与GUI一起工作的原因。在课堂上使用它时我做错了什么?

感谢您的回答。

1 个答案:

答案 0 :(得分:0)

你的问题是Gauge两者都是Canvas 的子类,而创建了一个或多个其他画布(奇怪的是,它是某个其他窗口的子画面)。

当您调用makeGauge时,会创建第二个画布并返回它。但是,您不保存引用,也不会在第二个画布上调用packplacegrid,因此它永远不会显示。

我不知道你打算用两幅画布做什么,但我猜你真的只想要一张。我建议创建这样的类,在创建画布时传递所有参数,以便您可以在一个步骤中创建仪表:

class Gauge(Canvas):
    def __init__(self, parent, value, gaugeType):
        Canvas.__init__(self, parent)
        self.makeGauge(value, gaugeType)

    def makeGauge(self, value, gaugeType):
        if gaugeType == "speed":
            self.configure(bg="black", width=400, height=217,
                           highlightthickness=0)
            self.create_arc([10, 5, 390, 395], start=0, extent=180,
                            fill="white")

        elif gaugeType == "rpm":
            ...

speedo = Gauge(bgMiddle, 150, "speed")
speedo.grid(...)

如果您想保留两个画布,或者您决定使Gauge继承其他内容,则需要保存makeGauge返回的引用,然后将其添加到显示中:

gauge = speedo.makeGauge(bgMiddle, 150, "speed")
gauge.grid(row=1, columnspan=5, sticky=E+N+S+W)