Skip to content

Commit

Permalink
added custom imports for wx 3/4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskiehl committed Aug 13, 2017
1 parent dc6386c commit 2289abf
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 46 deletions.
39 changes: 21 additions & 18 deletions gooey/gui/imageutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@
@author: Chris
'''

import wx
from gooey.gui.three_to_four import imageFromBitmap, bitmapFromImage




def _load_image(image_path):
try:
return wx.Bitmap(image_path)
except:
raise IOError('Invalid Image path')
try:
return wx.Bitmap(image_path)
except:
raise IOError('Invalid Image path')


def resize_bitmap(parent, _bitmap, target_height):
'''
Resizes a bitmap to a height of 89 pixels (the
size of the top panel), while keeping aspect ratio
in tact
'''
image = _bitmap.ConvertToImage()
_width, _height = image.GetSize()
if _height < target_height:
return wx.StaticBitmap(parent, -1, wx.Bitmap(image))
ratio = float(_width) / _height
image = image.Scale(target_height * ratio, target_height, wx.IMAGE_QUALITY_HIGH)
return wx.StaticBitmap(parent, -1, wx.Bitmap(image))

'''
Resizes a bitmap to a height of 89 pixels (the
size of the top panel), while keeping aspect ratio
in tact
'''
image = imageFromBitmap(_bitmap)
_width, _height = image.GetSize()
if _height < target_height:
return wx.StaticBitmap(parent, -1, bitmapFromImage(image))
ratio = float(_width) / _height
image = image.Scale(target_height * ratio, target_height, wx.IMAGE_QUALITY_HIGH)
return wx.StaticBitmap(parent, -1, bitmapFromImage(image))


if __name__ == '__main__':
pass
pass
47 changes: 47 additions & 0 deletions gooey/gui/three_to_four.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
Util for supporting WxPython 3 & 4
'''

import wx
try:
import wx.adv
except ImportError:
pass

isLatestVersion = wx.version().startswith('4')


class Constants:
if isLatestVersion:
WX_FONTSTYLE_NORMAL = wx.FONTSTYLE_NORMAL
WX_DP_DROPDOWN = wx.adv.DP_DROPDOWN
else:
WX_FONTSTYLE_NORMAL = wx.FONTWEIGHT_NORMAL
WX_DP_DROPDOWN = wx.DP_DROPDOWN


class Classes:
if isLatestVersion:
DatePickerCtrl = wx.adv.DatePickerCtrl
else:
DatePickerCtrl = wx.DatePickerCtrl




def imageFromBitmap(bitmap):
if isLatestVersion:
return bitmap.ConvertToImage()
else:
return wx.ImageFromBitmap(bitmap)


def bitmapFromImage(image):
if isLatestVersion:
return wx.Bitmap(image)
else:
return wx.BitmapFromImage(image)




65 changes: 40 additions & 25 deletions gooey/gui/util/wx_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,61 @@

import wx

from gooey.gui.three_to_four import Constants


styles = {
'h0': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False),
'h1': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False),
'h2': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False),
'bold': (wx.FONTFAMILY_DEFAULT, Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False)
}


def make_bold(statictext):
pointsize = statictext.GetFont().GetPointSize()
font = wx.Font(pointsize, wx.FONTFAMILY_DEFAULT,
wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False)
statictext.SetFont(font)
pointsize = statictext.GetFont().GetPointSize()
font = wx.Font(pointsize, *styles['bold'])
statictext.SetFont(font)


def dark_grey(statictext):
darkgray = (54, 54, 54)
statictext.SetForegroundColour(darkgray)
darkgray = (54, 54, 54)
statictext.SetForegroundColour(darkgray)


def h0(parent, label):
text = wx.StaticText(parent, label=label)
font_size = text.GetFont().GetPointSize()
font = wx.Font(font_size * 1.4, *(wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False))
text.SetFont(font)
return text
text = wx.StaticText(parent, label=label)
font_size = text.GetFont().GetPointSize()
font = wx.Font(font_size * 1.4, *styles['h0'])
text.SetFont(font)
return text


def h1(parent, label):
return _header(parent, label, (wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False))
return _header(parent, label, styles['h1'])


def h2(parent, label):
return _header(parent, label, (wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False))
return _header(parent, label, styles['h2'])


def _header(parent, label, styles):
text = wx.StaticText(parent, label=label)
font_size = text.GetFont().GetPointSize()
font = wx.Font(font_size * 1.2, *styles)
text.SetFont(font)
return text
text = wx.StaticText(parent, label=label)
font_size = text.GetFont().GetPointSize()
font = wx.Font(font_size * 1.2, *styles)
text.SetFont(font)
return text


def horizontal_rule(parent):
return _rule(parent, wx.LI_HORIZONTAL)
return _rule(parent, wx.LI_HORIZONTAL)

def vertical_rule(parent):
return _rule(parent, wx.LI_VERTICAL)

def _rule(parent, direction):
line = wx.StaticLine(parent, -1, style=direction)
line.SetSize((10, 10))
return line
def vertical_rule(parent):
return _rule(parent, wx.LI_VERTICAL)


def _rule(parent, direction):
line = wx.StaticLine(parent, -1, style=direction)
line.SetSize((10, 10))
return line
5 changes: 3 additions & 2 deletions gooey/gui/widgets/calender_dialog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
__author__ = 'Chris'

import wx
import wx.adv

from gooey.gui.util import wx_util

from gooey.gui.three_to_four import Classes, Constants


class CalendarDlg(wx.Dialog):
def __init__(self, parent):
Expand All @@ -13,7 +14,7 @@ def __init__(self, parent):
self.SetBackgroundColour('#ffffff')

self.ok_button = wx.Button(self, wx.ID_OK, label='Ok')
self.datepicker = wx.adv.DatePickerCtrl(self, style=wx.adv.DP_DROPDOWN)
self.datepicker = Classes.DatePickerCtrl(self, style=Constants.WX_DP_DROPDOWN)

vertical_container = wx.BoxSizer(wx.VERTICAL)
vertical_container.AddSpacer(10)
Expand Down
4 changes: 3 additions & 1 deletion gooey/gui/windows/runtime_display_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import wx
from gooey.gui.lang import i18n

from gooey.gui.three_to_four import Constants


class RuntimeDisplay(wx.Panel):
'''
Expand All @@ -22,7 +24,7 @@ def __init__(self, parent, **kwargs):
def set_font_style(self, style):
pointsize = self.cmd_textbox.GetFont().GetPointSize()
font = wx.Font(pointsize, style,
wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False)
Constants.WX_FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False)
self.cmd_textbox.SetFont(font)

def _init_properties(self):
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
'application with one line'),
license='MIT',
packages=find_packages(),
install_requires=[
'wxpython==5.7',
'Rx==1.5.9'
],
include_package_data=True,
dependency_links = ["http://www.wxpython.org/download.php"],
classifiers = [
Expand Down

0 comments on commit 2289abf

Please sign in to comment.