在我的程序中遇到RadioButtons问题

时间:2016-01-13 08:38:12

标签: python python-2.7 python-3.x tkinter radio-button

我有一个计算两点最短距离的程序。我试图使用radiobutton增加距离或减去结果的距离。我的错误说它需要6个参数和5个参数。我不知道如何解决这个问题。任何建议都会有所帮助,谢谢。

from Tkinter import *
from ttk import *
import ttk
import heapq


class Application:
    def __init__(self, master):
        self.create_widgets()
        self.trip19 = {}
        self.frameinMaintab()
        self.comboboxes()

    def create_widgets(self):
        self.notebook = Notebook(style='col.TNotebook')
        self.tabA = Frame(self.notebook)
        self.mainA = Notebook(self.tabA)
        self.tab1 = Frame(self.mainA, style='tab.TFrame')
        self.mainA.add(self.tab1, text="test")
        self.mainA.pack(fill='both', expand=1, padx=10, pady=9)
        self.notebook.add(self.tabA, text="testtab")
        self.notebook.pack(fill='both', expand=1, padx=0, pady=0)

    def comboboxes(self):

        self.combo1a = ttk.Combobox(self.tab1)
        self.combo1a.node_id = 'start'
        self.combo1a['values'] = ('a', 'b', 'w', 'x', 'y', 'z')
        self.combo1a.bind("<<ComboboxSelected>>", self.handler19a)
        self.combo1a.state(['readonly'])
        self.combo1a.place(x=120, y=20)
        self.combo1b = ttk.Combobox(self.tab1)  #
        self.combo1b.node_id = 'end'
        self.combo1b['values'] = ('a', 'b', 'w', 'x', 'y', 'z')
        self.combo1b.bind("<<ComboboxSelected>>", self.handler19b)
        self.combo1b.state(['readonly'])
        self.combo1b.place(x=120, y=50)

        self.var = IntVar()
        self.var2 = IntVar()

        self.rad= Radiobutton(self.tab1, text="-1",variable = self.var, value=1).place(x=200,y=100)
        self.rad2= Radiobutton(self.tab1, text="-2",variable = self.var, value=-2).place(x=200,y=130)
        self.rad3= Radiobutton(self.tab1, text="1",variable = self.var2, value=1).place(x=240,y=100)
        self.rad4= Radiobutton(self.tab1, text="2",variable = self.var2, value=2).place(x=240,y=130)

    def frameinMaintab(self):
        self.labelfont = ('Tahoma', 20)
        self.lblText19 = StringVar()
        self.labl1 = Label(self.tab1, textvariable= self.lblText19)
        self.labl1.config(font=self.labelfont, background= '#E0E2E3')
        self.labl1.place(x=10, y=30)

    def shortestPath(self, start, end):  # Algorithm
        queue, seen = [(7, start, [])], set()
        while True:
            (cost, v, path) = heapq.heappop(queue)
            if v not in seen:
                path = path + [v]
                seen.add(v)
                if v == end:
                    return cost, path
                for (next, c) in self.graph[v].iteritems():
                    heapq.heappush(queue, (cost + c, next, path))

    graph = {
        'a': {'w': 16, 'x': 9, 'y': 11},
        'b': {'w': 11, 'z': 8},
        'w': {'a': 16, 'b': 11, 'y': 4},
        'x': {'a': 9, 'y': 12, 'z': 17},
        'y': {'a': 11, 'w': 4, 'x': 12, 'z': 13},
        'z': {'a': 8, 'x': 17, 'y': 13},
    }

    def event_handler19(self, event, combobox, nodes, radio, radio2):
        nodes[combobox.node_id] = combobox.get()
        start, end = nodes.get('start'), nodes.get('end')
        ras = radio.get() + radio2.get()
        if start and end and ras:
            cost, path = self.shortestPath(start, end)

            cost = cost - ras
            self.lblText19.set(cost)
            self.labelmeter = Label(self.tab1,text= "Feet")
            self.labelmeter.place(x=10, y=70)

    def handler19a(self, event):  # interface function
        combobox = self.combo1a
        nodes = self.trip19
        radio = self.var
        return self.event_handler19(event, combobox, nodes, radio)

    def handler19b(self, event):  # interface function
        combobox = self.combo1b
        nodes = self.trip19
        radio2 = self.var2
        return self.event_handler19(event, combobox, nodes, radio2)


root = Tk()
root.title("")
root.geometry("400x400")
app = Application(root)
root.configure(background='#E0E2E3')
root.resizable(0, 0)
root.mainloop()

1 个答案:

答案 0 :(得分:2)

你定义了这样的函数:

def event_handler19(self, event, combobox, nodes, radio, radio2) # 6 Arguments, including the 'self'; None of them optional

你可以这样称呼你的功能:

return self.event_handler19(event, combobox, nodes, radio) # 5 Arguments, including the 'self'

和此:

return self.event_handler19(event, combobox, nodes, radio2) # 5 Arguments, including the 'self'

所以你用错误的参数调用你的函数。通过提供默认值使它们成为可选项,或者将其作为** kwargs传递。

示例:

def event_handler19(self, event, combobox, nodes, radio=None, radio2=None):
    if not radio:
         radio = self.var
    if not radio2:
         radio2 = self.var2
    # rest of your code

你会这样称呼它:

return self.event_handler19(event, combobox, nodes, radio=radio)

和:

return self.event_handler19(event, combobox, nodes, radio2=radio2)