Skip to content

Commit 4b0b0b6

Browse files
authored
fix(gui): resolve Tkinter RIGHT/LEFT constant collision (#1316)
Replace wildcard tkinter import with namespaced 'import tkinter as tk' in gui/romania_problem.py to avoid AIMA's RIGHT/LEFT (utils.py) shadowing tkinter constants, which caused _tkinter.TclError: bad side "-1".
1 parent 3504561 commit 4b0b0b6

1 file changed

Lines changed: 27 additions & 28 deletions

File tree

gui/romania_problem.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from copy import deepcopy
2-
from tkinter import *
2+
import tkinter as tk
33

44
from search import *
55
from utils import PriorityQueue
@@ -28,7 +28,7 @@ def create_map(root):
2828
width = 750
2929
height = 670
3030
margin = 5
31-
city_map = Canvas(root, width=width, height=height)
31+
city_map = tk.Canvas(root, width=width, height=height)
3232
city_map.pack()
3333

3434
# Since lines have to be drawn between particular points, we need to list
@@ -274,31 +274,31 @@ def make_rectangle(map, x0, y0, margin, city_name):
274274
x0 - 2 * margin,
275275
y0 - 2 * margin,
276276
text=city_name,
277-
anchor=E)
277+
anchor=tk.E)
278278
else:
279279
map.create_text(
280280
x0 - 2 * margin,
281281
y0 - 2 * margin,
282282
text=city_name,
283-
anchor=SE)
283+
anchor=tk.SE)
284284
city_coord.update({city_name: rect})
285285

286286

287287
def make_legend(map):
288288
rect1 = map.create_rectangle(600, 100, 610, 110, fill="white")
289-
text1 = map.create_text(615, 105, anchor=W, text="Un-explored")
289+
text1 = map.create_text(615, 105, anchor=tk.W, text="Un-explored")
290290

291291
rect2 = map.create_rectangle(600, 115, 610, 125, fill="orange")
292-
text2 = map.create_text(615, 120, anchor=W, text="Frontier")
292+
text2 = map.create_text(615, 120, anchor=tk.W, text="Frontier")
293293

294294
rect3 = map.create_rectangle(600, 130, 610, 140, fill="red")
295-
text3 = map.create_text(615, 135, anchor=W, text="Currently Exploring")
295+
text3 = map.create_text(615, 135, anchor=tk.W, text="Currently Exploring")
296296

297297
rect4 = map.create_rectangle(600, 145, 610, 155, fill="grey")
298-
text4 = map.create_text(615, 150, anchor=W, text="Explored")
298+
text4 = map.create_text(615, 150, anchor=tk.W, text="Explored")
299299

300300
rect5 = map.create_rectangle(600, 160, 610, 170, fill="dark green")
301-
text5 = map.create_text(615, 165, anchor=W, text="Final Solution")
301+
text5 = map.create_text(615, 165, anchor=tk.W, text="Final Solution")
302302

303303

304304
def tree_search(problem):
@@ -622,51 +622,50 @@ def reset_map():
622622

623623
# TODO: Add more search algorithms in the OptionMenu
624624
if __name__ == "__main__":
625-
global algo, start, goal, next_button
626-
root = Tk()
625+
root = tk.Tk()
627626
root.title("Road Map of Romania")
628627
root.geometry("950x1150")
629-
algo = StringVar(root)
630-
start = StringVar(root)
631-
goal = StringVar(root)
628+
algo = tk.StringVar(root)
629+
start = tk.StringVar(root)
630+
goal = tk.StringVar(root)
632631
algo.set("Breadth-First Tree Search")
633632
start.set('Arad')
634633
goal.set('Bucharest')
635634
cities = sorted(romania_map.locations.keys())
636-
algorithm_menu = OptionMenu(
635+
algorithm_menu = tk.OptionMenu(
637636
root,
638637
algo, "Breadth-First Tree Search", "Depth-First Tree Search",
639638
"Breadth-First Graph Search", "Depth-First Graph Search",
640639
"Uniform Cost Search", "A* - Search")
641-
Label(root, text="\n Search Algorithm").pack()
640+
tk.Label(root, text="\n Search Algorithm").pack()
642641
algorithm_menu.pack()
643-
Label(root, text="\n Start City").pack()
644-
start_menu = OptionMenu(root, start, *cities)
642+
tk.Label(root, text="\n Start City").pack()
643+
start_menu = tk.OptionMenu(root, start, *cities)
645644
start_menu.pack()
646-
Label(root, text="\n Goal City").pack()
647-
goal_menu = OptionMenu(root, goal, *cities)
645+
tk.Label(root, text="\n Goal City").pack()
646+
goal_menu = tk.OptionMenu(root, goal, *cities)
648647
goal_menu.pack()
649-
frame1 = Frame(root)
650-
next_button = Button(
648+
frame1 = tk.Frame(root)
649+
next_button = tk.Button(
651650
frame1,
652651
width=6,
653652
height=2,
654653
text="Next",
655654
command=on_click,
656655
padx=2,
657656
pady=2,
658-
relief=GROOVE)
659-
next_button.pack(side=RIGHT)
660-
reset_button = Button(
657+
relief=tk.GROOVE)
658+
next_button.pack(side=tk.RIGHT)
659+
reset_button = tk.Button(
661660
frame1,
662661
width=6,
663662
height=2,
664663
text="Reset",
665664
command=reset_map,
666665
padx=2,
667666
pady=2,
668-
relief=GROOVE)
669-
reset_button.pack(side=RIGHT)
670-
frame1.pack(side=BOTTOM)
667+
relief=tk.GROOVE)
668+
reset_button.pack(side=tk.RIGHT)
669+
frame1.pack(side=tk.BOTTOM)
671670
create_map(root)
672671
root.mainloop()

0 commit comments

Comments
 (0)