Tkinter从菜单栏中选择多个

时间:2018-06-28 11:25:49

标签: python python-3.x tkinter menubar

在tkinter中使菜单栏的多选复选框成为问题。我的代码生成一个选项列表,但没有用于检查/取消选中它们的字段:

SCREENSHOT

我试图实现与本博文中描述的外观和功能类似的功能:

Python 3.5: Assigning multiple variables using choices in TKInter dropdown menu

我的意思是从菜单栏级别创建一个选择列表。这是我的代码的一部分:

import twitter
import threading
import time
import tkinter as tk
from tkinter import *

class scanner(Frame):

def __init__(self, root = None):
    Frame.__init__(self,root)
    self.root = root
    self.init_window()

def init_window(self):
    root.grid_columnconfigure(0, weight = 1)
    root.grid_rowconfigure(0, weight = 1)

    #text field
    self.entry_1 = tk.Text(root)
    self.entry_1.grid(row = 0, column = 0, columnspan = 1, sticky = N+W+E+S)

    menu = Menu(self.root)
    self.root.config(menu = menu)
    time = Menu(menu)
    self.newValue = 30000
    timeToRun = IntVar()
    self.timeToRun = timeToRun
    time.add_radiobutton(label = '1 minute', value = 60000, variable = timeToRun, command = self.setValue)
    time.add_radiobutton(label = '5 minutes', value = 300000, variable = timeToRun, command = self.setValue)
    time.add_radiobutton(label = '15 minutes', value = 900000, variable = timeToRun, command = self.setValue)
    time.add_radiobutton(label = '30 minutes', value = 1800000, variable = timeToRun, command = self.setValue)
    time.add_radiobutton(label = '45 minutes', value = 2700000, variable = timeToRun, command = self.setValue)
    time.add_radiobutton(label = '1 hour', value = 3600000, variable = timeToRun, command = self.setValue)
    time.add_radiobutton(label = '2 hours', value = 7200000, variable = timeToRun, command = self.setValue)
    time.add_radiobutton(label = '5 hours', value = 18000000, variable = timeToRun, command = self.setValue)

    menu.add_cascade(label='Time', menu = time)

    #checkbox
    targets = Menu(menu)
    valueTargets = StringVar()
    self.valueTargets = valueTargets
    menu.add_cascade(label='Targets', menu = targets)

    self.listOfSources = []

    for choice in self.listOfSources:
        targets.add_checkbutton(label = choice, variable = valueTargets, onvalue = choice, offvalue = choice, command = self.modTarget)

我将不胜感激。

0 个答案:

没有答案