【问题标题】:Python GTK: How to make it so a checkbox determines if a window will open or notPython GTK:如何使复选框确定窗口是否打开
【发布时间】:2014-06-17 15:51:11
【问题描述】:

在我的程序中,我有一个烦人的弹出窗口,提醒用户去检查设置。我有一个复选框,如果选中,则应该永远不会再次显示弹出窗口。我不知道该怎么做。当复选框被选中时,我需要程序写入一个名为 tog_file.txt 的文本文件,说“真”。如果 tog_file.txt 中的文本为 true,则弹出窗口将永远不会再次显示。 代码:

def reminder():
    global tog
    tog = open("tog_file.txt", "r+b")
    tog_r = tog.read()
    if tog_r == "true":
        print "true"
    if tog_r == "":
        global reminder
        reminder = gtk.Window(gtk.WINDOW_TOPLEVEL)
        reminder.set_position(gtk.WIN_POS_CENTER)
        reminder.set_size_request(450, 205)
        reminder.set_title("Info")

        textbox = gtk.Label("Remember To Go Into The Settings Menu! There are Important Settings that Need to be Reviewed by you!")
        textbox.set_justify(gtk.JUSTIFY_CENTER) 
        textbox.set_line_wrap(True)   

        button = gtk.Button("Close")
        button.connect("clicked", reminder_close)

        toggle = gtk.CheckButton("Don't Show This Message Again")
        item_text = toggle.get_active()
        if item_text == False:
            print "no"
        if item_text == True:
            print "yea"
            tog.write("true")

        fixed = gtk.Fixed()
        fixed.put(button, 300, 160)
        fixed.put(toggle, 50, 165)
        fixed.put(textbox, 50, 50)

        reminder.add(fixed)
        reminder.show_all()

def reminder_close(self):
    reminder.destroy()

如果您需要任何其他信息,请发表评论。 感谢 StackOverflow :)

【问题讨论】:

  • if os.path.exists('file-path'):with open('file-path','r') as f:if f.read() == 'true':其他跳过弹出窗口:显示弹出窗口
  • @cox 你能发表回复而不是评论,这样我就可以说你回答了这个问题,所以我可以理解你的回答
  • @cox 这甚至与我正在寻找的东西都不接近。我需要能够通过 toggle.get_active() 判断何时在文件中写入“true”

标签: python linux terminal gtk


【解决方案1】:

我认为你想要 get_active() 方法。检查 button.get_active() => boolean 是否并采取相应措施。您也可以将无线电 bt 连接到“切换”信号,并在 on_toggle 函数中设置一个具有 bt 状态的类变量:if bt.get_active():self.ischeck=True else:self.ischeck = False 或短方式:self.ischeck = True if bt.get_active() else False

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多