23
23
import dragonpy
24
24
from dragonpy .core import configs
25
25
26
- try :
27
- # Python 3
28
- import queue
29
- import tkinter
30
- from tkinter import filedialog
31
- from tkinter import messagebox
32
- from tkinter import scrolledtext
33
- from tkinter import font as TkFont
34
- except ImportError :
26
+ if sys .version_info [0 ] == 2 :
35
27
# Python 2
36
28
import Queue as queue
37
- import Tkinter as tkinter
29
+ import Tkinter as tk
38
30
import tkFileDialog as filedialog
39
31
import tkMessageBox as messagebox
40
32
import ScrolledText as scrolledtext
41
33
import tkFont as TkFont
34
+ else :
35
+ # Python 3
36
+ import queue
37
+ import tkinter as tk
38
+ from tkinter import filedialog
39
+ from tkinter import messagebox
40
+ from tkinter import scrolledtext
41
+ from tkinter import font as TkFont
42
+
43
+
42
44
43
45
log = logging .getLogger (__name__ )
44
46
75
77
76
78
# sys.exit()
77
79
80
+ class RunButtonsFrame (tk .LabelFrame ):
81
+ def __init__ (self , master , ** kwargs ):
82
+ tk .LabelFrame .__init__ (self , master , text = "Run machines" )
83
+ self .grid (** kwargs )
84
+
85
+ self .machine_dict = master .machine_dict
86
+
87
+ self .var_machine = tk .StringVar ()
88
+ self .var_machine .set (configs .DRAGON64 )
89
+ for row , machine_name in enumerate (sorted (self .machine_dict )):
90
+ print (row , machine_name )
91
+ b = tk .Radiobutton (self , text = machine_name ,
92
+ variable = self .var_machine , value = machine_name )
93
+ b .grid (row = row , column = 1 , sticky = tk .W )
94
+
95
+ button_run = tk .Button (self ,
96
+ width = 25 ,
97
+ text = "run machine" ,
98
+ command = master .run_machine
99
+ )
100
+ button_run .grid (row = row + 1 , column = 1 )
78
101
79
102
80
- class GuiStarter (object ):
103
+ class GuiStarter (tk . Tk ):
81
104
def __init__ (self , cli_file , machine_dict ):
105
+ tk .Tk .__init__ (self )
106
+
82
107
self .cli_file = os .path .abspath (cli_file )
83
108
self .machine_dict = machine_dict
84
109
85
- self .root = tkinter .Tk (className = "STARTER" )
86
- self .root .geometry ("+%d+%d" % (
87
- self .root .winfo_screenwidth () * 0.1 , self .root .winfo_screenheight () * 0.1
110
+ self .geometry ("+%d+%d" % (
111
+ self .winfo_screenwidth () * 0.1 , self .winfo_screenheight () * 0.1
88
112
))
89
- self .root .title ("DragonPy v%s GUI starter" % dragonpy .__version__ )
90
-
91
- _row = - 1
113
+ self .title ("DragonPy v%s GUI starter" % dragonpy .__version__ )
92
114
93
- self .var_machine = tkinter .StringVar ()
94
- self .var_machine .set (configs .DRAGON64 )
95
- for machine_name , data in self .machine_dict .items ():
96
- b = tkinter .Radiobutton (self .root , text = machine_name ,
97
- variable = self .var_machine , value = machine_name )
98
- _row += 1
99
- b .grid (row = _row , column = 1 , columnspan = 2 , sticky = tkinter .W )
100
-
101
- _row += 1
115
+ _row = 0
102
116
103
- self .var_verbosity = tkinter .StringVar ()
117
+ self .var_verbosity = tk .StringVar ()
104
118
self .var_verbosity .set (VERBOSITY_DEFAULT )
105
- w = tkinter .Label (self . root , text = "Verbosity:" )
106
- w .grid (row = _row , column = 1 , sticky = tkinter .E )
107
- w = tkinter .OptionMenu (
108
- self . root , self .var_verbosity ,
119
+ w = tk .Label (self , text = "Verbosity:" )
120
+ w .grid (row = _row , column = 1 , sticky = tk .E )
121
+ w = tk .OptionMenu (
122
+ self , self .var_verbosity ,
109
123
* VERBOSITY_STRINGS
110
124
)
111
125
w .config (width = 20 )
112
- w .grid (row = _row , column = 2 , sticky = tkinter .W )
126
+ w .grid (row = _row , column = 2 , sticky = tk .W )
113
127
114
128
_row += 1
115
129
116
- button_run = tkinter .Button (self .root ,
117
- width = 25 ,
118
- text = "run" ,
119
- command = self .run_machine
120
- )
121
- button_run .grid (row = _row , column = 1 , columnspan = 2 )
122
-
123
- _row += 1
124
-
125
- w = tkinter .Label (
126
- self .root ,
130
+ w = tk .Label (
131
+ self ,
127
132
text = "CLI script:\n %r" % self .cli_file ,
128
- justify = tkinter .LEFT
133
+ justify = tk .LEFT
129
134
)
130
135
w .grid (row = _row , column = 1 , columnspan = 2 )
131
136
132
- self .root .update ()
137
+ self .add_widgets ()
138
+
139
+ self .update ()
140
+
141
+ def add_widgets (self ):
142
+ padding = 5
143
+ defaults = {
144
+ "ipadx" : padding , # add internal padding in x direction
145
+ "ipady" : padding , # add internal padding in y direction
146
+ "padx" : padding , # add padding in x direction
147
+ "pady" : padding , # add padding in y direction
148
+ "sticky" : tk .NSEW , # stick to the cell boundary
149
+ }
150
+
151
+ self .run_buttons = RunButtonsFrame (self , column = 0 , row = 0 , ** defaults )
152
+ # self.inputs = Inputs(self, column=0, row=1, **defaults)
153
+ # self.actions = Buttons(self, column=1, row=0, rowspan=2, **defaults)
133
154
134
155
def run_machine (self ):
135
- machine_name = self .var_machine .get ()
156
+ machine_name = self .run_buttons . var_machine .get ()
136
157
print ("run: %r" % machine_name )
137
158
138
159
verbosity = self .var_verbosity .get ()
@@ -165,9 +186,6 @@ def run_machine(self):
165
186
print ("Startup CLI with: %s" % " " .join (cmd_args [1 :]))
166
187
subprocess .Popen (cmd_args )
167
188
168
- def mainloop (self ):
169
- """ for standalone usage """
170
- self .root .mainloop ()
171
189
172
190
def start_gui (cli_file , machine_dict ):
173
191
gui = GuiStarter (cli_file , machine_dict )
@@ -176,4 +194,4 @@ def start_gui(cli_file, machine_dict):
176
194
177
195
if __name__ == "__main__" :
178
196
from dragonpy .core .cli import main
179
- main ()
197
+ main (confirm_exit = False )
0 commit comments