|
36 | 36 | # Speed control in GUI does not have any effect -- fix it. |
37 | 37 |
|
38 | 38 | from utils import * |
39 | | -import random, copy |
| 39 | +import random |
| 40 | +import copy |
40 | 41 |
|
41 | 42 | #______________________________________________________________________________ |
42 | 43 |
|
@@ -537,87 +538,5 @@ def score(env): |
537 | 538 | True |
538 | 539 | """ |
539 | 540 |
|
540 | | -#______________________________________________________________________________ |
541 | | -# GUI - Graphical User Interface for Environments |
542 | | -# If you do not have Tkinter installed, either get a new installation of Python |
543 | | -# (Tkinter is standard in all new releases), or delete the rest of this file |
544 | | -# and muddle through without a GUI. |
545 | | - |
546 | | -import Tkinter as tk |
547 | | - |
548 | | -class EnvGUI(tk.Tk, object): |
549 | | - |
550 | | - def __init__(self, env, title = 'AIMA GUI', cellwidth=50, n=10): |
551 | | - |
552 | | - # Initialize window |
553 | | - |
554 | | - super(EnvGUI, self).__init__() |
555 | | - self.title(title) |
556 | | - |
557 | | - # Create components |
558 | | - |
559 | | - canvas = EnvCanvas(self, env, cellwidth, n) |
560 | | - toolbar = EnvToolbar(self, env, canvas) |
561 | | - for w in [canvas, toolbar]: |
562 | | - w.pack(side="bottom", fill="x", padx="3", pady="3") |
563 | | - |
564 | | - |
565 | | -class EnvToolbar(tk.Frame, object): |
566 | | - |
567 | | - def __init__(self, parent, env, canvas): |
568 | | - super(EnvToolbar, self).__init__(parent, relief='raised', bd=2) |
569 | | - |
570 | | - # Initialize instance variables |
571 | | - |
572 | | - self.env = env |
573 | | - self.canvas = canvas |
574 | | - self.running = False |
575 | | - self.speed = 1.0 |
576 | | - |
577 | | - # Create buttons and other controls |
578 | | - |
579 | | - for txt, cmd in [('Step >', self.env.step), |
580 | | - ('Run >>', self.run), |
581 | | - ('Stop [ ]', self.stop), |
582 | | - ('List things', self.list_things), |
583 | | - ('List agents', self.list_agents)]: |
584 | | - tk.Button(self, text=txt, command=cmd).pack(side='left') |
585 | | - |
586 | | - tk.Label(self, text='Speed').pack(side='left') |
587 | | - scale = tk.Scale(self, orient='h', |
588 | | - from_=(1.0), to=10.0, resolution=1.0, |
589 | | - command=self.set_speed) |
590 | | - scale.set(self.speed) |
591 | | - scale.pack(side='left') |
592 | | - |
593 | | - def run(self): |
594 | | - print('run') |
595 | | - self.running = True |
596 | | - self.background_run() |
597 | | - |
598 | | - def stop(self): |
599 | | - print('stop') |
600 | | - self.running = False |
601 | | - |
602 | | - def background_run(self): |
603 | | - if self.running: |
604 | | - self.env.step() |
605 | | - # ms = int(1000 * max(float(self.speed), 0.5)) |
606 | | - #ms = max(int(1000 * float(self.delay)), 1) |
607 | | - delay_sec = 1.0 / max(self.speed, 1.0) # avoid division by zero |
608 | | - ms = int(1000.0 * delay_sec) # seconds to milliseconds |
609 | | - self.after(ms, self.background_run) |
610 | | - |
611 | | - def list_things(self): |
612 | | - print("Things in the environment:") |
613 | | - for thing in self.env.things: |
614 | | - print("%s at %s" % (thing, thing.location)) |
615 | | - |
616 | | - def list_agents(self): |
617 | | - print("Agents in the environment:") |
618 | | - for agt in self.env.agents: |
619 | | - print("%s at %s" % (agt, agt.location)) |
620 | 541 |
|
621 | | - def set_speed(self, speed): |
622 | | - self.speed = float(speed) |
623 | 542 |
|
0 commit comments