首页 > 解决方案 > 如何使单选按钮连接到文本文件中的一行并使其显示在框架上

问题描述

我创建了一个程序,它应该允许用户选择他们想要投票的候选人。我已经在一个单独的 python 文件上创建了这个类,但是在同一个文件夹中,并且使用了 import filename * 语句。当我单击按钮为候选人投票时,单选按钮和文本不会出现在框架上。

class LoginSuccessful(tk.Frame):

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

    label = tk.Label(self, text="Greenwich Student Union Voting System", bg="blue", fg="white",
                     width="200", height="2", font=controller.title_font)
    label.pack(side="top", fill="x", pady=10)

    button = tk.Button(self, text="View Candidates", height="2", width="30",
                       command=lambda: controller.show_frame("ViewCandidates"))
    button.pack()

    label = tk.Label(self, text="", )
    label.pack(side="top", fill="x", pady=10)

    button1 = tk.Button(self, text="Vote for Candidates", height="2", width="30",
                        command=lambda: controller.show_frame("VoteForPresident"))

上面的代码在单独的 python 文件中,但是我使用 import 语句将 python 文件链接在一起

class VoteForPresident(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Vote for your President ", bg="red", fg="white",
                     width="200", height="2", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

    def selection(self):
        radio = IntVar()

        selection = msg.showinfo("Submit", "You have chosen President Barn as your preference " + str(radio.get()))

        with open("PresidentCandidates.txt", "r") as f:
            label = tk.Label(self, text=f.read())
            label.pack(side="top", fill="x", pady=10)

        r1 = Radiobutton(text="1ST PREFERENCE", variable=radio, value=1, command=selection)
        r1.pack(side="top", fill="x", pady=10)

        r2 = Radiobutton(text="2ND PREFERENCE", variable=radio, value=2, command=selection)
        r2.pack(side="top", fill="x", pady=10)

        r3 = Radiobutton(text="3RD PREFERENCE", variable=radio, value=3, command=selection)
        r3.pack(side="top", fill="x", pady=10)

        r4 = Radiobutton(text="4TH PREFERENCE", variable=radio, value=4, command=selection)
        r4.pack(side="top", fill="x", pady=10)

        label = tk.Label(self, text="")
        label.pack(side="top", fill="x", pady=10)

        button = tk.Button(self, text="Back", height="2", width="30",
                       command=lambda: 
self.controller.show_frame("LoginSuccessful"))
    button.pack()

标签: python-3.xbuttontkinterradio

解决方案


推荐阅读