Created
June 7, 2020 07:00
-
-
Save mattvh/eeb736469cee457aaedac778fe936e5b to your computer and use it in GitHub Desktop.
Writing a GUI Application with Python and Py2App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from setuptools import setup | |
APP = ['subreddit.py'] | |
DATA_FILES = [] | |
OPTIONS = { | |
'argv_emulation': True, | |
'site_packages': True, | |
#'iconfile': 'appicon.icns', | |
'packages': ['wx', 'requests'], | |
'plist': { | |
'CFBundleName': 'Subreddit Viewer', | |
} | |
} | |
setup( | |
app=APP, | |
data_files=DATA_FILES, | |
options={'py2app': OPTIONS}, | |
setup_requires=['py2app'], | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
import requests, json | |
import webbrowser | |
class AppWindow(wx.Frame): | |
def __init__(self): | |
super().__init__(parent=None, title='/r/programming', size=(650, 400)) | |
self.items = [] | |
self.panel = wx.Panel(self) | |
sizer = wx.BoxSizer(wx.VERTICAL) | |
self.table = wx.ListCtrl(self, size=(-1, 400), style=wx.LC_REPORT | wx.BORDER_SUNKEN) | |
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.row_clicked, self.table) | |
self.build_table() | |
sizer.Add(self.table, 0, wx.EXPAND) | |
self.SetSizer(sizer) | |
self.menu_bar = wx.MenuBar() | |
self.SetMenuBar(self.menu_bar) | |
self.Show() | |
def build_table(self): | |
self.table.InsertColumn(0, 'Title', width=400) | |
self.table.InsertColumn(1, 'User', width=100) | |
self.table.InsertColumn(2, 'Comments', width=100) | |
response = requests.get( | |
'https://www.reddit.com/r/programming.json', | |
headers = {'User-agent': 'Fancy Reddit GUI 1.0'} | |
).text | |
data = json.loads(response) | |
i = 0 | |
for item in data['data']['children']: | |
self.items.append(item['data']) | |
self.table.InsertItem(i, item['data']['title']) | |
self.table.SetItem(i, 1, item['data']['author']) | |
self.table.SetItem(i, 2, str(item['data']['num_comments'])) | |
i += 1 | |
def row_clicked(self, event): | |
row = event.GetIndex() | |
item = self.items[row] | |
url = item['url'] | |
webbrowser.open_new_tab(url) | |
if __name__ == '__main__': | |
app = wx.App(False) | |
window = AppWindow() | |
app.MainLoop() |
I am doing this for a image steganography program made with Tkinter. Do I need to put “tk” in “‘packages’”?
yes if it doesn't detect automatically
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am doing this for a image steganography program made with Tkinter. Do I need to put “tk” in “‘packages’”?