Python 我的程序中的单选按钮有问题

Python 我的程序中的单选按钮有问题,python,python-2.7,python-3.x,tkinter,radio-button,Python,Python 2.7,Python 3.x,Tkinter,Radio Button,我有一个计算两点最短距离的程序。我正在尝试使用单选按钮来增加距离或从结果中减去距离。我的错误是需要6个参数,并给出了5个参数。我不知道如何着手解决这个问题。任何建议都会有帮助,谢谢 from Tkinter import * from ttk import * import ttk import heapq class Application: def __init__(self, master): self.create_widgets() self

我有一个计算两点最短距离的程序。我正在尝试使用单选按钮来增加距离或从结果中减去距离。我的错误是需要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()
从Tkinter导入*
从ttk导入*
导入ttk
进口heapq
班级申请:
定义初始(自我,主):
self.create_widgets()
self.trip19={}
self.frameinMaintab()
self.combobox()
def创建_小部件(自):
self.notebook=笔记本(style='col.TNotebook')
self.tabA=框架(self.notebook)
self.mainA=笔记本(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组合框(自身):
self.combo1a=ttk.Combobox(self.tab1)
self.combo1a.node_id='start'
self.combo1a['values']=('a','b','w','x','y','z')
self.combo1a.bind(“,self.handler19a)
self.combo1a.state(['readonly']))
自身位置(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(“,self.handler19b)
self.combo1b.state(['readonly']))
自身位置(x=120,y=50)
self.var=IntVar()
self.var2=IntVar()
self.rad=Radiobutton(self.tab1,text=“-1”,变量=self.var,值=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=单选按钮(self.tab1,text=“1”,variable=self.var2,value=1)。放置(x=240,y=100)
self.rad4=单选按钮(self.tab1,text=“2”,variable=self.var2,value=2)。放置(x=240,y=130)
def框架维护(自身):
self.labelfont=('Tahoma',20)
self.lblText19=StringVar()
self.labl1=Label(self.tab1,textvariable=self.lblText19)
self.lab1.config(font=self.labelfont,background='#E0E2E3')
自标签1.位置(x=10,y=30)
def最短路径(自、开始、结束):#算法
队列,seen=[(7,start,[])],set()
尽管如此:
(成本,v,路径)=heapq.heapop(队列)
如果看不到v:
路径=路径+[v]
见.添加(v)
如果v==结束:
返回成本,路径
对于self.graph[v].iteritems()中的(next,c):
heappush(队列,(成本+c,下一步,路径))
图={
'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(自身、事件、组合框、节点、收音机、收音机2):
节点[combobox.node_id]=combobox.get()
start,end=nodes.get('start'),nodes.get('end'))
ras=radio.get()+radio2.get()
如果开始、结束和ras:
成本,路径=自身。最短路径(开始,结束)
成本=成本-ras
self.lblText19.set(成本)
self.labelmeter=标签(self.tab1,text=“英尺”)
自标定仪位置(x=10,y=70)
def handler19a(自身、事件):#接口功能
combobox=self.combo1a
节点=self.19
radio=self.var
返回self.event_handler19(事件、组合框、节点、收音机)
def handler19b(自身、事件):#接口功能
combobox=self.combo1b
节点=self.19
radio2=self.var2
返回self.event_handler19(事件、组合框、节点、radio2)
root=Tk()
根标题(“”)
根几何(“400x400”)
app=应用程序(根)
root.configure(后台='#E0E2E3')
根目录。可调整大小(0,0)
root.mainloop()

您对函数的定义如下:

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, radio=radio)
你可以这样调用你的函数:

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, radio=radio)
这是:

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
你会这样称呼它:

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, radio=radio)
以及:


您对函数的定义如下:

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, radio=radio)
你可以这样调用你的函数:

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, radio=radio)
这是:

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
你会这样称呼它:

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, radio=radio)
以及:


请从控制台“return self.event_handler19(event,combobox,nodes,radio)TypeError:event_handler19()在
91
行上正好接受了6个参数(5个给定参数),您只传递了
4个
参数,您还没有传递
radio2
与第
97行相同
请从控制台发布错误”返回self.event\u handler19(event,combobox,nodes,radio)TypeError:event\u handler19()在
91
行上正好接受了6个参数(5个给定参数),您只传递了
4个
radio2
参数,您没有传递的
radio2
与在
97
行上的参数相同,我对python非常陌生,我不知道您说的意思“通过提供默认值使它们成为可选的“。我也不知道如何使用**kwargs,但我现在正在查找它。”。ThanksI编辑了答案并添加了一个示例。希望能有帮助。非常感谢,但当我选择组合框并单击单选按钮时,结果似乎没有显示出来。恐怕我对TkInter业务不太感兴趣。但是查看代码时,总是将
trip19
作为节点传递。但是,此值永远不会更改。因此
如果start和end以及ras:
将始终计算为false,因为不存在“start”