我在哪里可以声明我的StringVar来跟踪它并将它与OptionBox一起使用

时间:2016-01-14 23:25:03

标签: python tkinter widget

我一直在尝试声明这个StringVar,这样当我更改一个选项框的内容时,另一个选项框的内容会更新。虽然这已被多次询问,但我似乎无法使其发挥作用。

通常会说你应该在组合框之前声明它,写下更新'例程并在与' def'相同的行下面声明跟踪。这很有用,但是我也希望在许多其他例程中访问我的字符串var的值,因此用“自我”声明它。'我似乎无法让这两件事情同时发挥作用。

以下是我的一些代码,请注意self.varRun和底层例程,因为这些代码必须相互协作(同时仍然可以在此类的其他例程中使用varRuns) 。

class createViewer(Tk.Frame):

    def __init__(self, parent, controller):
        Tk.Frame.__init__(self,parent)

        leftFrame = Tk.Frame(self)
        leftFrame.pack(side="left")

        botLeftFrame = Tk.Frame(leftFrame)
        botLeftFrame.pack(side="bottom")

        Import = Tk.Button(botLeftFrame)
        Import["text"] = "Import Frame"
        Import["command"] = self.importRun
        Import.pack(side="left", padx = 5, pady = 10)

        Load = Tk.Button(botLeftFrame)
        Load["text"] = "Load Frame"
        Load["command"] = self.loadRun
        Load.pack(side="left", padx = 5, pady = 10)

        runLabel = Tk.Label(botLeftFrame, text = "Select Run")
        runLabel.pack(side="left", padx = 5, pady = 10)


        cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Public\dbsDetectorBookingSystem.accdb')
        cursor = cnxn.cursor()
        self.Runs = []
        cursor.execute("SELECT RunFilePath FROM tblRuns")
        rows = cursor.fetchall()
        for row in rows:
            self.Runs.append(row.RunFilePath)

        self.varRun = Tk.StringVar()
        self.varRun.set("None selected")
        self.varRun.trace('w', self.runSelectionChanged)

        self.SelectRun = Tk.OptionMenu(botLeftFrame, self.varRun, *self.Runs)
        self.SelectRun.pack(side="left", padx = 5, pady = 10)

        frameLabel = Tk.Label(botLeftFrame, text = "Select Frame")
        frameLabel.pack(side="left", padx = 5, pady = 10)

        self.varFrame = Tk.StringVar()
        self.Frames = ["holder","holder2"]
        self.SelectFrame = Tk.OptionMenu(botLeftFrame, self.varFrame, *self.Frames)
        self.varFrame.set("None selected")
        self.SelectFrame.pack(side="left", padx = 5, pady = 10)

        Home = Tk.Button(botLeftFrame, text="Home",command=lambda: controller.show_frame(createHome))
        Home.pack(side="left", padx = 5, pady = 10)


        topLeftFrame = Tk.Frame(leftFrame)
        topLeftFrame.pack(side="top", expand = True, fill = "both")

        img = Image.open("C:\Users\Jacob\workspace2\Blank2.png")
        blkImg = ImageTk.PhotoImage(img)
        self.ImageViewer = Tk.Label(topLeftFrame, image = blkImg)
        self.ImageViewer.image = blkImg
        self.ImageViewer.pack(side = "left", padx = 5, pady = 10)

        rightFrame = Tk.Frame(self, bg = "white")
        rightFrame.pack(side="right", padx = 5, pady = 10)

        self.alphaCount = 0
        self.betaCount = 0
        self.gammaCount = 0
        self.alphaText = Tk.StringVar()
        self.betaText = Tk.StringVar()
        self.gammaText = Tk.StringVar()

        self.alphaText.set("Alpha: 0")
        self.betaText.set("Beta: 0")
        self.gammaText.set("Gamma: 0")

        self.alpha = Tk.Label(rightFrame, textvariable = self.alphaText, bg = "white")
        self.alpha.pack(side="top", padx = 5, pady = 5)
        self.beta = Tk.Label(rightFrame, textvariable = self.betaText, bg = "white")
        self.beta.pack(side="top", padx = 5, pady = 5)
        self.gamma = Tk.Label(rightFrame, textvariable = self.gammaText, bg = "white")
        self.gamma.pack(side="top", padx = 5, pady = 5)

    def runSelectionChanged(self,*args):
        cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Public\dbsDetectorBookingSystem.accdb')
        cursor = cnxn.cursor()
        cursor.execute("SELECT RunFilePath, RunID FROM tblRuns")
        rows = cursor.fetchall()
        for row in rows:
            if row.RunFilePath == self.varRun.get():
                chosenRunID = row.RunID

        cursor.execute("SELECT LocalFilePath FROM tblFrames WHERE RunID=?") (chosenRunID)
        self.userFrames = cursor.fetchall()
        for frame in self.userFrames:
            self.Frames.append(frame)

0 个答案:

没有答案