Robot Framework
dialogs_py.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import sys
17 from threading import current_thread
18 import time
19 
20 try:
21  from Tkinter import (Button, Entry, Frame, Label, Listbox, TclError,
22  Toplevel, Tk, BOTH, END, LEFT, W)
23 except ImportError:
24  from tkinter import (Button, Entry, Frame, Label, Listbox, TclError,
25  Toplevel, Tk, BOTH, END, LEFT, W)
26 
27 
28 class _TkDialog(Toplevel):
29 
32  _left_button = 'OK'
33 
36  _right_button = 'Cancel'
37 
38  def __init__(self, message, value=None, **extra):
39  self._prevent_execution_with_timeouts_prevent_execution_with_timeouts()
40  self._parent_parent = self._get_parent_get_parent()
41  Toplevel.__init__(self, self._parent_parent)
42  self._initialize_dialog_initialize_dialog()
43  self._create_body_create_body(message, value, **extra)
44  self._create_buttons_create_buttons()
45  self._result_result = None
46 
48  if 'linux' not in sys.platform and current_thread().name != 'MainThread':
49  raise RuntimeError('Dialogs library is not supported with '
50  'timeouts on Python on this platform.')
51 
52  def _get_parent(self):
53  parent = Tk()
54  parent.withdraw()
55  return parent
56 
57  def _initialize_dialog(self):
58  self.title('Robot Framework')
59  self.grab_setgrab_set()
60  self.protocol("WM_DELETE_WINDOW", self._close_close)
61  self.bind("<Escape>", self._close_close)
62  self.minsize(250, 80)
63  self.geometry("+%d+%d" % self._get_center_location_get_center_location())
64  self._bring_to_front_bring_to_front()
65 
66  def grab_set(self, timeout=30):
67  maxtime = time.time() + timeout
68  while time.time() < maxtime:
69  try:
70  # Fails at least on Linux if mouse is hold down.
71  return Toplevel.grab_set(self)
72  except TclError:
73  pass
74  raise RuntimeError('Failed to open dialog in %s seconds. One possible '
75  'reason is holding down mouse button.' % timeout)
76 
78  x = (self.winfo_screenwidth() - self.winfo_reqwidth()) // 2
79  y = (self.winfo_screenheight() - self.winfo_reqheight()) // 2
80  return x, y
81 
82  def _bring_to_front(self):
83  self.lift()
84  self.attributes('-topmost', True)
85  self.after_idle(self.attributes, '-topmost', False)
86 
87  def _create_body(self, message, value, **extra):
88  frame = Frame(self)
89  Label(frame, text=message, anchor=W, justify=LEFT, wraplength=800).pack(fill=BOTH)
90  selector = self._create_selector_create_selector(frame, value, **extra)
91  if selector:
92  selector.pack(fill=BOTH)
93  selector.focus_set()
94  frame.pack(padx=5, pady=5, expand=1, fill=BOTH)
95 
96  def _create_selector(self, frame, value):
97  return None
98 
99  def _create_buttons(self):
100  frame = Frame(self)
101  self._create_button_create_button(frame, self._left_button_left_button,
102  self._left_button_clicked_left_button_clicked)
103  self._create_button_create_button(frame, self._right_button_right_button,
104  self._right_button_clicked_right_button_clicked)
105  frame.pack()
106 
107  def _create_button(self, parent, label, callback):
108  if label:
109  button = Button(parent, text=label, width=10, command=callback)
110  button.pack(side=LEFT, padx=5, pady=5)
111 
112  def _left_button_clicked(self, event=None):
113  if self._validate_value_validate_value():
114  self._result_result = self._get_value_get_value()
115  self._close_close()
116 
117  def _validate_value(self):
118  return True
119 
120  def _get_value(self):
121  return None
122 
123  def _close(self, event=None):
124  # self.destroy() is not enough on Linux
125  self._parent_parent.destroy()
126 
127  def _right_button_clicked(self, event=None):
128  self._result_result = self._get_right_button_value_get_right_button_value()
129  self._close_close()
130 
132  return None
133 
134  def show(self):
135  self.wait_window(self)
136  return self._result_result
137 
138 
140 
143  _right_button = None
144 
145 
147 
148  def __init__(self, message, default='', hidden=False):
149  _TkDialog.__init__(self, message, default, hidden=hidden)
150 
151  def _create_selector(self, parent, default, hidden):
152  self._entry_entry = Entry(parent, show='*' if hidden else '')
153  self._entry_entry.insert(0, default)
154  self._entry_entry.select_range(0, END)
155  return self._entry_entry
156 
157  def _get_value(self):
158  return self._entry_entry.get()
159 
160 
162 
163  def __init__(self, message, values):
164  _TkDialog.__init__(self, message, values)
165 
166  def _create_selector(self, parent, values):
167  self._listbox_listbox = Listbox(parent)
168  for item in values:
169  self._listbox_listbox.insert(END, item)
170  self._listbox_listbox.config(width=0)
171  return self._listbox_listbox
172 
173  def _validate_value(self):
174  return bool(self._listbox_listbox.curselection())
175 
176  def _get_value(self):
177  return self._listbox_listbox.get(self._listbox_listbox.curselection())
178 
179 
181 
182  def __init__(self, message, values):
183  _TkDialog.__init__(self, message, values)
184 
185  def _create_selector(self, parent, values):
186  self._listbox_listbox = Listbox(parent, selectmode='multiple')
187  for item in values:
188  self._listbox_listbox.insert(END, item)
189  self._listbox_listbox.config(width=0)
190  return self._listbox_listbox
191 
192  def _get_value(self):
193  selected_values = [self._listbox_listbox.get(i) for i in self._listbox_listbox.curselection()]
194  return selected_values
195 
196 
198 
201  _left_button = 'PASS'
202 
205  _right_button = 'FAIL'
206 
207  def _get_value(self):
208  return True
209 
211  return False
def _create_selector(self, parent, default, hidden)
Definition: dialogs_py.py:151
def __init__(self, message, default='', hidden=False)
Definition: dialogs_py.py:148
def __init__(self, message, values)
Definition: dialogs_py.py:163
def _create_selector(self, parent, values)
Definition: dialogs_py.py:166
def _close(self, event=None)
Definition: dialogs_py.py:123
def __init__(self, message, value=None, **extra)
Definition: dialogs_py.py:38
def grab_set(self, timeout=30)
Definition: dialogs_py.py:66
def _create_button(self, parent, label, callback)
Definition: dialogs_py.py:107
def _create_selector(self, frame, value)
Definition: dialogs_py.py:96
def _create_body(self, message, value, **extra)
Definition: dialogs_py.py:87
def _right_button_clicked(self, event=None)
Definition: dialogs_py.py:127
def _left_button_clicked(self, event=None)
Definition: dialogs_py.py:112