0
我是python的新手。我试图在框架上显示我的代码的结果。此外,当从TabA1中的组合框中选取一个字母时,它会影响tabA2上的结果。我不知道如何去解决这些问题。任何建议都会很棒。需要关于如何在框架上显示结果的建议
from Tkinter import *
from ttk import *
import ttk
import heapq
class Application:
def __init__(self, master):
self.create_widgets()
self.trip = {}
def create_widgets(self):
self.notebook = Notebook() # Widgets
self.style = Style()
self.style.configure('frame.TFrame', background='grey')
self.frame = Frame(style='frame.TFrame')
self.frame.place(height=40, width=70, x=158, y=109)
self.frame.config()
self.tabA = Frame(self.notebook)
self.tabB = Frame(self.notebook)
self.tab3 = Frame(self.notebook)
self.mainA = Notebook(self.tabA)
self.tabA1 = Frame(self.mainA)
self.tabA2 = Frame(self.mainA)
self.mainA.add(self.tabA1, text = "tabA1")
self.mainA.add(self.tabA2, text = "tabA2")
self.mainA.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
self.notebook.add(self.tabA, text = "tabA")
self.notebook.add(self.tabB, text= "tabB")
self.notebook.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
self.combo1= ttk.Combobox(self.tabA1)
self.combo1.node_id = 'start'
self.combo1['values'] = ('a','b','w','x','y','z')
self.combo1.bind("<<ComboboxSelected>>", self.handler1)
self.combo1.pack()
self.combo2= ttk.Combobox(self.tabA1)
self.combo2.node_id = 'end'
self.combo2['values'] = ('a','b','w','x','y','z')
self.combo2.bind("<<ComboboxSelected>>", self.handler2)
self.combo2.pack()
self.combotab2= ttk.Combobox(self.tabA2)
self.combotab2.node_id = 'start'
self.combotab2['values'] = ('a','b','w','x','y','z')
self.combotab2.bind("<<ComboboxSelected>>", self.handler15)
self.combotab2.pack()
self.combotab22= ttk.Combobox(self.tabA2)
self.combotab22.node_id = 'end'
self.combotab22['values'] = ('a','b','w','x','y','z')
self.combotab22.bind("<<ComboboxSelected>>", self.handler215)
self.combotab22.pack()
def shortestPath(self,start, end): # Algorithm
queue,seen = [(0, 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': {'b': 8, 'x': 17, 'y': 13},
}
def event_handler(self, event, combobox, nodes):
nodes[combobox.node_id] = combobox.get()
# call shortestPath() if both a source and destination node are defined
start, end = nodes.get('start'), nodes.get('end')
if start and end:
cost, path = self.shortestPath(start, end)
print cost, path
def event_handler2(self,event, combobox, nodes):
nodes[combobox.node_id] = combobox.get()
# call shortestPath() if both a source and destination node are defined
start, end = nodes.get('start'), nodes.get('end')
if start and end:
cost, path = self.shortestPath(start, end)
print cost, path
def handler1(self,event): # interface function
combobox = self.combo1
nodes=self.trip
return self.event_handler(event, combobox, nodes)
def handler2(self, event): # interface function
combobox = self.combo2
nodes = self.trip
return self.event_handler(event, combobox, nodes)
def handler15(self,event): # interface function
combobox = self.combotab2
nodes= self.trip
return self.event_handler2(event, combobox, nodes)
def handler215(self, event): # interface function
combobox = self.combotab22
nodes = self.trip
return self.event_handler2(event, combobox, nodes)
root = Tk()
root.title("Test")
root.geometry("400x400")
app = Application(root)
root.mainloop()
感谢1000倍,工作。 – wallace