class UI(object): def __init__(self, args): self.root = Tk() self.args = args # Make it invisible # - 0 size, top left corner, transparent. self.root.attributes('-alpha', 0.0) self.root.geometry('0x0+0+0') # "Show" window again and lift it to top so it can get focus, # otherwise dialogs will end up behind other windows self.root.deiconify() self.root.lift() self.root.focus_force() def selectFolder(self, **options): """ Show a folder selection window for the user to import an addon """ if self.args.addon_path is None: selected_addon_dir_path = tkFileDialog.askdirectory( parent=self.root, **options) else: selected_addon_dir_path = self.args.addon_path return selected_addon_dir_path def success(self, action): message = addon_import_success_message.format(action.lower()) print(message) if self.args.addon_path is None: tkMessageBox.showinfo(message=message) def cancel(self, action): message = addon_import_cancel_message.format(action.lower()) print(message) if self.args.addon_path is None: tkMessageBox.showinfo(message=message) sys.exit(0) def fail(self, msg): print(msg) if self.args.addon_path is None: tkMessageBox.showerror(message=msg) sys.exit(1) def confirmAction(self, addon_name): proceed = True info = addon_select_info_message.format(addon_name, self.args.project, self.args.workspace) print(info) if self.args.addon_path is None: proceed = tkMessageBox.askokcancel('Attention!', info) print(proceed) return proceed
class TokenInputGUI(object): def show_entry_fields(self, event=None): self.code = self.e1.get() self.master.withdraw() self.master.quit() def quit(self): self.master.quit() self.code = None def doGUI(self, hostname=None): self.master = Tk() self.master.title('Blessclient - MFA') textmsg = 'Enter your AWS MFA code: ' if hostname: textmsg = 'Enter your AWS MFA code to connect to {}: '.format( hostname) Label(self.master, text=textmsg).grid(row=0) self.e1 = Entry(self.master) self.e1.grid(row=0, column=1, padx=4) Button(self.master, text='OK', command=self.show_entry_fields, default=ACTIVE).grid(row=3, column=0, sticky=W, pady=4) Button(self.master, text='Cancel', command=self.quit).grid(row=3, column=1, sticky=W, pady=4) self.center() self.master.bind('<Return>', self.show_entry_fields) self.master.lift() self.master.attributes('-topmost', True) self.master.focus_force() self.e1.focus_set() if platform.system() == 'Darwin': # Hack to get the GUI dialog focused in OSX # https://stackoverflow.com/questions/1892339/how-to-make-a-tkinter-window-jump-to-the-front/37235492#37235492 tmpl = 'tell application "System Events" to set frontmost of every process whose unix id is {} to true' script = tmpl.format(os.getpid()) subprocess.check_call(['/usr/bin/osascript', '-e', script]) mainloop() # http://stackoverflow.com/questions/3352918/how-to-center-a-window-on-the-screen-in-tkinter def center(self): self.master.update_idletasks() w = self.master.winfo_screenwidth() h = self.master.winfo_screenheight() size = tuple( int(_) for _ in self.master.geometry().split('+')[0].split('x')) x = w / 2 - size[0] / 2 y = h / 2 - size[1] / 2 self.master.geometry("%dx%d+%d+%d" % (size + (x, y)))
def main(): root = Tk() root.title("Physic test") root.grid() root.grid_columnconfigure(0, weight=1) root.grid_rowconfigure(0, weight=1) root.geometry("500x500") cnv = CrossTest(root) cnv.grid(column=0, row=0, sticky="NEWS") root.mainloop()
def main(args): (work, rest, geometry, debug, server, port, mode) = parse(args) logging.basicConfig(level="DEBUG" if debug else "INFO", style='{', format=FORMAT) app = Tk() app.title("Typing Watcher") if geometry: app.geometry(geometry) task = Task(master=app, work=work, rest=rest, server=server, port=port, mode=mode) task.pack() app.mainloop()
class GUIScreen(BaseScreen): """ :type root: Tk """ def __init__(self): super(GUIScreen, self).__init__() urwid.set_encoding('utf8') self.root = None self.size = (180, 60) self.title = "Taurus Status" self.text = None self.font = None def get_cols_rows(self): """ Dummy cols and rows :return: """ return self.size def _start(self): super(GUIScreen, self)._start() self.root = Tk() self.root.geometry("%sx%s" % (self.size[0] * 7, self.size[1] * 15)) self.root.bind("<Configure>", self.resize) if platform.system() == 'Windows': self.root.bind("<Control-MouseWheel>", self.change_font) else: self.root.bind("<Control-4>", self.change_font) self.root.bind("<Control-5>", self.change_font) self.root.protocol("WM_DELETE_WINDOW", self.closed_window) self.text = Text(self.root, font="TkFixedFont", wrap=Tkinter.NONE, state=Tkinter.DISABLED, background="black", foreground="light gray") self.text.pack(side=Tkinter.LEFT, fill=Tkinter.BOTH, expand=Tkinter.YES) self.font = tkFont.Font(self.root, self.text.cget("font")) self.text.config(font=self.font) self.__prepare_tags() def _stop(self): if self.root: self.root.destroy() super(GUIScreen, self)._stop() def change_font(self, event): """ Change font event handler :param event: :return: """ min_size = 1 cur_size = self.font['size'] inc = 1 if cur_size > 0 else -1 if event.num == 4 or event.delta > 0: self.font.configure(size=cur_size + inc) self.resize(event) if event.num == 5 or event.delta < 0: if cur_size != min_size * inc: self.font.configure(size=cur_size - inc) self.resize(event) def resize(self, event): """ Resize screen :param event: :return: """ (cwdth, chght) = (self.font.measure(' '), self.font.metrics("linespace")) logging.debug("Font: %s", (cwdth, chght)) width = int( math.floor( (self.text.winfo_width() - float(cwdth) / 2) / float(cwdth))) height = int(math.floor(self.text.winfo_height() / float(chght))) self.size = (width, height) self.root.title(self.title + " %sx%s" % self.size) def closed_window(self): self.root.destroy() self.root = None def draw_screen(self, size, canvas): """ :param size: :type canvas: urwid.Canvas """ if not self.root: raise ManualShutdown("GUI window was closed") # enable changes self.text.config(state=Tkinter.NORMAL) self.text.delete("1.0", Tkinter.END) for idx, row in enumerate(canvas.content()): pos = 0 for part in row: txt = part[2] if isinstance(txt, six.text_type): strlen = len(txt) else: strlen = len(txt.decode('utf8')) self.text.insert(Tkinter.END, txt) if part[0] is not None: self.text.tag_add(part[0], "%s.%s" % (idx + 1, pos), "%s.%s" % (idx + 1, pos + strlen)) pos += strlen self.text.insert(Tkinter.END, "\n") # disable changes self.text.config(state=Tkinter.DISABLED) self.root.update() def __translate_tcl_color(self, style): if style == 'default': return None elif style == "light magenta": return "magenta" elif style == "light red": return "red" elif style == "brown": return "dark orange" else: return style def __prepare_tags(self): for name, style in six.iteritems(self._palette): # NOTE: not sure which index use, used [0] bgc = self.__translate_tcl_color(style[0].background) fgc = self.__translate_tcl_color(style[0].foreground) self.text.tag_configure(name, background=bgc, foreground=fgc)
class GUIScreen(BaseScreen): """ :type root: Tk """ def __init__(self): super(GUIScreen, self).__init__() urwid.set_encoding('utf8') self.root = None self.size = (180, 60) self.title = "Taurus Status" self.text = None self.font = None def get_cols_rows(self): """ Dummy cols and rows :return: """ return self.size def _start(self): super(GUIScreen, self)._start() self.root = Tk() self.root.geometry("%sx%s" % (self.size[0] * 7, self.size[1] * 15)) self.root.bind("<Configure>", self.resize) if platform.system() == 'Windows': self.root.bind("<Control-MouseWheel>", self.change_font) else: self.root.bind("<Control-4>", self.change_font) self.root.bind("<Control-5>", self.change_font) self.root.protocol("WM_DELETE_WINDOW", self.closed_window) self.text = Text(self.root, font="TkFixedFont", wrap=Tkinter.NONE, state=Tkinter.DISABLED, background="black", foreground="light gray") self.text.pack(side=Tkinter.LEFT, fill=Tkinter.BOTH, expand=Tkinter.YES) self.font = tkFont.Font(self.root, self.text.cget("font")) self.text.config(font=self.font) self.__prepare_tags() def _stop(self): if self.root: self.root.destroy() super(GUIScreen, self)._stop() def change_font(self, event): """ Change font event handler :param event: :return: """ min_size = 1 cur_size = self.font['size'] inc = 1 if cur_size > 0 else -1 if event.num == 4 or event.delta > 0: self.font.configure(size=cur_size + inc) self.resize(event) if event.num == 5 or event.delta < 0: if cur_size != min_size * inc: self.font.configure(size=cur_size - inc) self.resize(event) def resize(self, event): """ Resize screen :param event: :return: """ (cwdth, chght) = (self.font.measure(' '), self.font.metrics("linespace")) logging.debug("Font: %s", (cwdth, chght)) width = int(math.floor((self.text.winfo_width() - float(cwdth) / 2) / float(cwdth))) height = int(math.floor(self.text.winfo_height() / float(chght))) self.size = (width, height) self.root.title(self.title + " %sx%s" % self.size) def closed_window(self): self.root.destroy() self.root = None def draw_screen(self, size, canvas): """ :param size: :type canvas: urwid.Canvas """ if not self.root: raise ManualShutdown("GUI window was closed") # enable changes self.text.config(state=Tkinter.NORMAL) self.text.delete("1.0", Tkinter.END) for idx, row in enumerate(canvas.content()): pos = 0 for part in row: txt = part[2] if isinstance(txt, six.text_type): strlen = len(txt) else: strlen = len(txt.decode('utf8')) self.text.insert(Tkinter.END, txt) if part[0] is not None: self.text.tag_add(part[0], "%s.%s" % (idx + 1, pos), "%s.%s" % (idx + 1, pos + strlen)) pos += strlen self.text.insert(Tkinter.END, "\n") # disable changes self.text.config(state=Tkinter.DISABLED) self.root.update() def __translate_tcl_color(self, style): if style == 'default': return None elif style == "light magenta": return "magenta" elif style == "light red": return "red" elif style == "brown": return "dark orange" else: return style def __prepare_tags(self): for name, style in six.iteritems(self._palette): # NOTE: not sure which index use, used [0] bgc = self.__translate_tcl_color(style[0].background) fgc = self.__translate_tcl_color(style[0].foreground) self.text.tag_configure(name, background=bgc, foreground=fgc)
class UI(object): def __init__(self, args): self.root = Tk() # Make it invisible # - 0 size, top left corner, transparent. self.root.attributes('-alpha', 0.0) self.root.geometry('0x0+0+0') try: qmde_icon = os.path.join(args.devkit, 'res', 'q_logo.ico') self.root.iconbitmap(bitmap=qmde_icon) except TclError: pass # "Show" window again and lift it to top so it can get focus, # otherwise dialogs will end up behind other windows self.root.deiconify() self.root.lift() self.root.focus_force() def selectFolder(self, **options): """ Show a folder selection window for the user to import/export private pre-built libraries """ return tkFileDialog.askdirectory(parent=self.root, **options) def selectFile(self, **options): """ Show a folder selection window for the user to import/export private pre-built libraries """ return tkFileDialog.askopenfilename(parent=self.root, **options) def success(self, action): message = 'Library {} operation succesful'.format(action.lower()) print(message) tkMessageBox.showinfo(message=message) def cancel(self, action): message = 'Library {} operation cancelled'.format(action.lower()) print(message) tkMessageBox.showinfo(message=message) sys.exit(0) def fail(self, msg): print(msg) tkMessageBox.showerror(message=msg) sys.exit(1) def confirmAction(self, action, header, archive): proceed = True header_base = os.path.basename(header) archive_base = os.path.basename(archive) info = "About to {} files:\n--{}\n--{}\n\n" \ .format(action.lower(), header_base, archive_base) question = "Proceed?" print(info + question) proceed = tkMessageBox.askokcancel('Attention!', info + question) print(proceed) return proceed def proceedDstFileExists(self, dst_dir, dst_file): dst_file = os.path.join(dst_dir, dst_file) proceed = True if os.path.isfile(dst_file): info = 'File: \n--{}\n\nalready exists at destination' \ .format(dst_file) question = "Proceed?" proceed = tkMessageBox.askokcancel('Attention!', info + question) return proceed
from six.moves.tkinter import (Tk, RAISED, Canvas, BOTH, HORIZONTAL, VERTICAL) from widgets import (AutoPanedWindow) if __name__ == "__main__": r = Tk() r.title("AutoPanedWindow example") a = AutoPanedWindow(r, sashrelief=RAISED, orient=HORIZONTAL) a.pack(fill=BOTH, expand=True) a.add(Canvas(a, bg="red"), sticky="NESW") a1 = AutoPanedWindow(a, sashrelief=RAISED, orient=VERTICAL) a.add(a1, sticky="NESW") blue = Canvas(a1, bg="blue") a1.add(blue, sticky="NESW") green = Canvas(a1, bg="green") a1.add(green, sticky="NESW") a1.add(Canvas(a1, bg="brown"), after=blue, sticky="NESW") yellow = Canvas(a, bg="yellow") a.add(yellow, sticky="NESW") a.add(Canvas(a, bg="pink"), before=yellow, sticky="NESW") r.geometry("800x600") r.mainloop()