jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/614577 )
Change subject: [4.0] remove Python 2 related code
......................................................................
[4.0] remove Python 2 related code
Change-Id: Id9f6fb4f6934cb94e7c1e6c39993f354013c704e
---
M pywikibot/userinterfaces/gui.py
1 file changed, 76 insertions(+), 87 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, but someone else must approve
D3r1ck01: Looks good to me, but someone else must approve
Zhuyifei1999: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/userinterfaces/gui.py b/pywikibot/userinterfaces/gui.py
index 23d27f4..6eebc62 100644
--- a/pywikibot/userinterfaces/gui.py
+++ b/pywikibot/userinterfaces/gui.py
@@ -1,41 +1,35 @@
# -*- coding: utf-8 -*-
"""
-A window with a unicode textfield where the user can edit.
+A window with a textfield where the user can edit.
Useful for editing the contents of an article.
"""
#
-# (C) Pywikibot team, 2003-2019
+# (C) Pywikibot team, 2003-2020
#
# Distributed under the terms of the MIT license.
#
-from __future__ import absolute_import, division, unicode_literals
+import tkinter
+from tkinter.scrolledtext import ScrolledText
+from tkinter import simpledialog as tkSimpleDialog
import pywikibot
from pywikibot import __url__
-from pywikibot.tools import PY2, PYTHON_VERSION, UnicodeType
+from pywikibot.tools import PYTHON_VERSION
-if not PY2:
- import tkinter as Tkinter
- from tkinter.scrolledtext import ScrolledText
- from tkinter import simpledialog as tkSimpleDialog
-else:
- import Tkinter
- import tkSimpleDialog
-
- from ScrolledText import ScrolledText
# T164163: Fix idlelib import in Python 3.6
if PYTHON_VERSION >= (3, 6):
from idlelib import (
search as SearchDialog,
replace as ReplaceDialog,
- configdialog as configDialog
)
from idlelib.config import idleConf
+ from idlelib.configdialog import ConfigDialog
from idlelib.multicall import MultiCallCreator
else:
- from idlelib import SearchDialog, ReplaceDialog, configDialog
+ from idlelib import SearchDialog, ReplaceDialog
+ from idlelib.configDialog import ConfigDialog
from idlelib.configHandler import idleConf
from idlelib.MultiCall import MultiCallCreator
@@ -69,7 +63,7 @@
# override defaults with any user-specified settings
textcf.update(kwargs)
- ScrolledText.__init__(self, master, **textcf)
+ super().__init__(master, **textcf)
def _initialize_config(self, Theme):
"""Fix idleConf.GetHighlight method for different Python
releases."""
@@ -218,14 +212,13 @@
"""
if hasattr(self, '_highlight') and self._highlight == s:
try:
- if self.get(Tkinter.SEL_FIRST, Tkinter.SEL_LAST) == s:
+ if self.get(tkinter.SEL_FIRST, tkinter.SEL_LAST) == s:
return self.find_selection_event(None)
- else:
- # user must have changed the selection
- found = self.tag_nextrange('found', Tkinter.SEL_LAST)
- except Tkinter.TclError:
+ # user must have changed the selection
+ found = self.tag_nextrange('found', tkinter.SEL_LAST)
+ except tkinter.TclError:
# user must have unset the selection
- found = self.tag_nextrange('found', Tkinter.INSERT)
+ found = self.tag_nextrange('found', tkinter.INSERT)
if not found:
# at last occurrence, scroll back to the top
found = self.tag_nextrange('found', 1.0)
@@ -235,14 +228,14 @@
# find all occurrences of string s;
# adapted from O'Reilly's Python in a Nutshell
# remove previous uses of tag 'found', if any
- self.tag_remove('found', '1.0', Tkinter.END)
+ self.tag_remove('found', '1.0', tkinter.END)
if s:
self._highlight = s
# start from the beginning (and when we come to the end, stop)
idx = '1.0'
while True:
# find next occurrence, exit loop if no more
- idx = self.search(s, idx, nocase=1, stopindex=Tkinter.END)
+ idx = self.search(s, idx, nocase=1, stopindex=tkinter.END)
if not idx:
break
# index right after the end of the occurrence
@@ -256,12 +249,13 @@
found = self.tag_nextrange('found', 1.0)
if found:
self.do_highlight(found[0], found[1])
+ return None
def do_highlight(self, start, end):
"""Select and show the text from index start to index
end."""
self.see(start)
- self.tag_remove(Tkinter.SEL, '1.0', Tkinter.END)
- self.tag_add(Tkinter.SEL, start, end)
+ self.tag_remove(tkinter.SEL, '1.0', tkinter.END)
+ self.tag_add(tkinter.SEL, start, end)
self.focus_set()
def goto_line_event(self, event):
@@ -275,9 +269,10 @@
return 'break'
self.mark_set('insert', '%d.0' % lineno)
self.see('insert')
+ return None
-class EditBoxWindow(Tkinter.Frame):
+class EditBoxWindow(tkinter.Frame):
"""Edit box window."""
@@ -285,42 +280,42 @@
"""Initializer."""
if parent is None:
# create a new window
- parent = Tkinter.Tk()
+ parent = tkinter.Tk()
self.parent = parent
- Tkinter.Frame.__init__(self, parent)
+ super().__init__(parent)
self.editbox = MultiCallCreator(TextEditor)(self, **kwargs)
- self.editbox.pack(side=Tkinter.TOP)
+ self.editbox.pack(side=tkinter.TOP)
self.editbox.add_bindings()
self.bind('<<open-config-dialog>>', self.config_dialog)
- bottom = Tkinter.Frame(parent)
+ bottom = tkinter.Frame(parent)
# lower left subframe with a textfield and a Search button
- bottom_left_frame = Tkinter.Frame(bottom)
- self.textfield = Tkinter.Entry(bottom_left_frame)
- self.textfield.pack(side=Tkinter.LEFT, fill=Tkinter.X, expand=1)
+ bottom_left_frame = tkinter.Frame(bottom)
+ self.textfield = tkinter.Entry(bottom_left_frame)
+ self.textfield.pack(side=tkinter.LEFT, fill=tkinter.X, expand=1)
- buttonSearch = Tkinter.Button(bottom_left_frame, text='Find next',
+ buttonSearch = tkinter.Button(bottom_left_frame, text='Find next',
command=self.find)
- buttonSearch.pack(side=Tkinter.RIGHT)
- bottom_left_frame.pack(side=Tkinter.LEFT, expand=1)
+ buttonSearch.pack(side=tkinter.RIGHT)
+ bottom_left_frame.pack(side=tkinter.LEFT, expand=1)
# lower right subframe which will contain OK and Cancel buttons
- bottom_right_frame = Tkinter.Frame(bottom)
+ bottom_right_frame = tkinter.Frame(bottom)
- buttonOK = Tkinter.Button(bottom_right_frame, text='OK',
+ buttonOK = tkinter.Button(bottom_right_frame, text='OK',
command=self.pressedOK)
- buttonCancel = Tkinter.Button(bottom_right_frame, text='Cancel',
+ buttonCancel = tkinter.Button(bottom_right_frame, text='Cancel',
command=parent.destroy)
- buttonOK.pack(side=Tkinter.LEFT, fill=Tkinter.X)
- buttonCancel.pack(side=Tkinter.RIGHT, fill=Tkinter.X)
- bottom_right_frame.pack(side=Tkinter.RIGHT, expand=1)
+ buttonOK.pack(side=tkinter.LEFT, fill=tkinter.X)
+ buttonCancel.pack(side=tkinter.RIGHT, fill=tkinter.X)
+ bottom_right_frame.pack(side=tkinter.RIGHT, expand=1)
- bottom.pack(side=Tkinter.TOP)
+ bottom.pack(side=tkinter.TOP)
# create a toplevel menu
- menubar = Tkinter.Menu(self.parent)
+ menubar = tkinter.Menu(self.parent)
- findmenu = Tkinter.Menu(menubar)
+ findmenu = tkinter.Menu(menubar)
findmenu.add_command(label='Find',
command=self.editbox.find_event,
accelerator='Ctrl+F',
@@ -342,7 +337,7 @@
underline=0)
menubar.add_cascade(label='Find', menu=findmenu, underline=0)
- editmenu = Tkinter.Menu(menubar)
+ editmenu = tkinter.Menu(menubar)
editmenu.add_command(label='Cut',
command=self.editbox.cut,
accelerator='Ctrl+X',
@@ -365,7 +360,7 @@
accelerator='Esc')
menubar.add_cascade(label='Edit', menu=editmenu, underline=0)
- optmenu = Tkinter.Menu(menubar)
+ optmenu = tkinter.Menu(menubar)
optmenu.add_command(label='Settings...',
command=self.config_dialog,
underline=0)
@@ -375,12 +370,11 @@
self.parent.config(menu=menubar)
self.pack()
- def edit(self, text, jumpIndex=None, highlight=None):
+ def edit(self, text: str, jumpIndex=None, highlight=None):
"""
Provide user with editor to modify text.
@param text: the text to be edited
- @type text: str
@param jumpIndex: position at which to put the caret
@type jumpIndex: int
@param highlight: each occurrence of this substring will be highlighted
@@ -391,11 +385,11 @@
"""
self.text = None
# put given text into our textarea
- self.editbox.insert(Tkinter.END, text)
+ self.editbox.insert(tkinter.END, text)
# wait for user to push a button which will destroy (close) the window
# enable word wrap
- self.editbox.tag_add('all', '1.0', Tkinter.END)
- self.editbox.tag_config('all', wrap=Tkinter.WORD)
+ self.editbox.tag_add('all', '1.0', tkinter.END)
+ self.editbox.tag_config('all', wrap=tkinter.WORD)
# start search if required
if highlight:
self.find_all(highlight)
@@ -412,7 +406,7 @@
def find_all(self, target):
"""Perform find all operation."""
- self.textfield.insert(Tkinter.END, target)
+ self.textfield.insert(tkinter.END, target)
self.editbox.find_all(target)
def find(self):
@@ -424,7 +418,7 @@
def config_dialog(self, event=None):
"""Show config dialog."""
- configDialog.ConfigDialog(self, 'Settings')
+ ConfigDialog(self, 'Settings')
def pressedOK(self):
"""
@@ -433,12 +427,7 @@
Called when user pushes the OK button.
Saves the buffer into a variable, and closes the window.
"""
- self.text = self.editbox.get('1.0', Tkinter.END)
- # if the editbox contains ASCII characters only, get() will
- # return string, otherwise unicode (very annoying). We only want
- # it to return unicode, so we work around this.
- if PY2 and isinstance(self.text, str):
- self.text = UnicodeType(self.text)
+ self.text = self.editbox.get('1.0', tkinter.END)
self.parent.destroy()
def debug(self, event=None):
@@ -448,7 +437,7 @@
# the following class isn't used anywhere in the framework: ####
-class ListBoxWindow(object):
+class ListBoxWindow:
"""List box window."""
@@ -466,21 +455,21 @@
"""Initializer."""
if parent is None:
# create a new window
- parent = Tkinter.Tk()
+ parent = tkinter.Tk()
self.parent = parent
# selectable: only one item
- self.listbox = Tkinter.Listbox(parent, selectmode=Tkinter.SINGLE)
+ self.listbox = tkinter.Listbox(parent, selectmode=tkinter.SINGLE)
# put list into main frame, using all available space
- self.listbox.pack(anchor=Tkinter.CENTER, fill=Tkinter.BOTH)
+ self.listbox.pack(anchor=tkinter.CENTER, fill=tkinter.BOTH)
# lower subframe which will contain one button
- self.bottom_frame = Tkinter.Frame(parent)
- self.bottom_frame.pack(side=Tkinter.BOTTOM)
+ self.bottom_frame = tkinter.Frame(parent)
+ self.bottom_frame.pack(side=tkinter.BOTTOM)
- buttonOK = Tkinter.Button(self.bottom_frame, text='OK',
+ buttonOK = tkinter.Button(self.bottom_frame, text='OK',
command=self.pressedOK)
- buttonOK.pack(side=Tkinter.LEFT, fill=Tkinter.X)
+ buttonOK.pack(side=tkinter.LEFT, fill=tkinter.X)
# idea: set title to cur_disambiguation
def list(self, list):
@@ -494,20 +483,20 @@
if len(list[i]) + len(str(i)) > maxbreite:
maxbreite = len(list[i]) + len(str(i))
# show list as formerly in DOS-window
- self.listbox.insert(Tkinter.END, str(i) + ' - ' + list[i])
+ self.listbox.insert(tkinter.END, str(i) + ' - ' + list[i])
# set optimized height & width
self.listbox.config(height=laenge, width=maxbreite + 2)
# wait for user to push a button which will destroy (close) the window
return self.list
-class Tkdialog(object):
+class Tkdialog:
"""The dialog window for image info."""
def __init__(self, photo_description, photo, filename):
"""Initializer."""
- self.root = Tkinter.Tk()
+ self.root = tkinter.Tk()
# "%dx%d%+d%+d" % (width, height, xoffset, yoffset)
self.root.geometry('%ix%i+10-10' % (pywikibot.config.tkhorsize,
pywikibot.config.tkvertsize))
@@ -522,32 +511,32 @@
# --Init of the widgets
# The image
self.image = self.get_image(self.photo, 800, 600)
- self.image_panel = Tkinter.Label(self.root, image=self.image)
+ self.image_panel = tkinter.Label(self.root, image=self.image)
self.image_panel.image = self.image
# The filename
- self.filename_label = Tkinter.Label(self.root,
+ self.filename_label = tkinter.Label(self.root,
text='Suggested filename')
- self.filename_field = Tkinter.Entry(self.root, width=100)
- self.filename_field.insert(Tkinter.END, filename)
+ self.filename_field = tkinter.Entry(self.root, width=100)
+ self.filename_field.insert(tkinter.END, filename)
# The description
- self.description_label = Tkinter.Label(self.root,
+ self.description_label = tkinter.Label(self.root,
text='Suggested description')
- self.description_scrollbar = Tkinter.Scrollbar(self.root,
- orient=Tkinter.VERTICAL)
- self.description_field = Tkinter.Text(self.root)
- self.description_field.insert(Tkinter.END, photo_description)
+ self.description_scrollbar = tkinter.Scrollbar(self.root,
+ orient=tkinter.VERTICAL)
+ self.description_field = tkinter.Text(self.root)
+ self.description_field.insert(tkinter.END, photo_description)
self.description_field.config(
- state=Tkinter.NORMAL, height=12, width=100, padx=0, pady=0,
- wrap=Tkinter.WORD, yscrollcommand=self.description_scrollbar.set)
+ state=tkinter.NORMAL, height=12, width=100, padx=0, pady=0,
+ wrap=tkinter.WORD, yscrollcommand=self.description_scrollbar.set)
self.description_scrollbar.config(command=self.description_field.yview)
# The buttons
- self.ok_button = Tkinter.Button(self.root, text='OK',
+ self.ok_button = tkinter.Button(self.root, text='OK',
command=self.ok_file)
- self.skip_button = Tkinter.Button(self.root, text='Skip',
+ self.skip_button = tkinter.Button(self.root, text='Skip',
command=self.skip_file)
# --Start grid
@@ -586,7 +575,7 @@
def ok_file(self):
"""The user pressed the OK button."""
self.filename = self.filename_field.get()
- self.photo_description = self.description_field.get(0.0, Tkinter.END)
+ self.photo_description = self.description_field.get(0.0, tkinter.END)
self.root.destroy()
def skip_file(self):
@@ -598,7 +587,7 @@
"""Activate the dialog.
@return: new description, name, and if the image is skipped
- @rtype: tuple of (unicode, unicode, bool)
+ @rtype: tuple of (str, str, bool)
"""
self.root.mainloop()
return self.photo_description, self.filename, self.skip
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/614577
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id9f6fb4f6934cb94e7c1e6c39993f354013c704e
Gerrit-Change-Number: 614577
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged