0

这是我的问题:我制作了一个 tkinter 程序来计算我妻子的创作成本。她缝制配饰。到目前为止,我的程序运行良好。如果这种面料已经存在于某种目录中,我会在 JSON 文件中进行搜索。

当我的目录具有相同的结构名称时,我想用单选按钮制作一个弹出窗口。通过检查正确的单选按钮,我选择了正确的面料。因此,我提取了相同结构名称的字典,当我键入结构名称条目然后单击 Enter 时,它会将字典发送到在 for 循环中创建单选按钮的弹出窗口。

问题来了,我的单选按钮制作正确,但无法到达那里状态,也无法检查哪个按钮......

首先,这是字典:

{"Tissu 1": {"Type": "Tissu", "Matière": "coton", "Prix du coupon": "8",
    "Laise du coupon (cm)": "150",
    "Prix au mètre (€)": 8.0,
    "Fournisseur": "lol"},
"Tissu 2": {"Type": "Tissu", "Matière": "molleton", "Prix du coupon": "10",
    "Laise du coupon (cm)": "150", 
    "Prix au mètre (€)": 10.0, 
    "Fournisseur": "lol"},
"Tissu 3": {"Type": "Tissu", "Matière": "coton", "Prix du coupon": "12", 
    "Laise du coupon (cm)": "150", 
    "Prix au mètre (€)": 12.0, 
    "Fournisseur": "mol"}}

这是我的代码:

首先是主类:

    class ProgramCalcul(tk.Tk):

        def __init__(self, *args, **kwargs):

        self.frames = {}

            for F in (StartPage, CalculTissuPage, CalculMercPage):

                frame = F(container, self)

                self.frames[F] = frame

                frame.grid(row = 0, column = 0, sticky = "nsew")

            self.show_frameSP(StartPage)

        def show_frameSP(self, cont):
            frame = self.frames[cont]

            frame.tkraise()

然后我有一个互相计算页面的类

这是织物计算页面(为了便于阅读,我删除了一些条目和其他小部件,因为它们工作正常):

class CalculTissuPage(tk.Frame):
    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        tissuType = tk.StringVar()
        TissuNameEntry = tk.Entry(self, textvariable = tissuType, width = 50)
        TissuNameEntry.grid(row = 2, padx = 20, pady = 5, sticky = "ew")

        def keyPress(arg):
            checkCatalogueTissu(TissuNameEntry.get())
        TissuNameEntry.bind('<Return>', keyPress)

        def checkCatalogueTissu(txt):

            tmpDict = {}

            with open("catalogue tissu.json") as json_file:
                data = json.load(json_file)

                dictLen = len(data)
                for i in range(1, dictLen+1):

                    if "Tissu {}".format(i) not in data:
                        pass

                    else:
                        if data["Tissu {}".format(i)]["Matière"] == txt:

                            tmpDict["Tissu {}".format(i)] = data["Tissu {}".format(i)]

            popupRadio(tmpDict)

        def setText(txtPrix, txtFournisseur, txtLaise, txtLong):
            couponPrixEntry.delete(0,tk.END)
            couponPrixEntry.insert(0,txtPrix)
            fournisseurEntry.delete(0, tk.END)
            fournisseurEntry.insert(0, txtFournisseur)
            laiseDimEntry.delete(0, tk.END)
            laiseDimEntry.insert(0, txtLaise)
            longCouponEntry.delete(0,tk.END)
            longCouponEntry.insert(0,txtLong)

最后是创建弹出窗口的函数,其中出现单选按钮:

def popupRadio(tmpDict):

    popup = tk.Toplevel()
    popup.wm_title("!")

    popupVar = tk.IntVar()

    def checkState(popupVar):
        print(popupVar.get())


    for (key, val) in tmpDict.items():

        tk.Radiobutton(popup, text = key, variable = popupVar, value = val, command = checkState(popupVar)).grid()
        print(key, popupVar.get())

    chk = ttk.Button(popup, text = "Select this", command = checkState(popupVar))
    chk.grid()

    popup.mainloop()

当我运行它时, checkState() 方法似乎自动运行,然后 chk 按钮不执行任何操作......我可以检查单选按钮但没有任何反应(即使我在单选按钮设置中传递了 command=chechState。我想要进一步检查哪个按钮以在我的 setText() 方法中使用此值。

目前,这是 print() 在我的控制台中发送的输出:

0
Tissu 1 0
0
Tissu 3 0
0

所以我在这里,自从我尝试了我在网上找到的所有东西以来已经有 2 天了,但什么都没有......

一点帮助将不胜感激

PS:对不起,如果我的英语不完美:)

4

0 回答 0