Skip to content

Commit 5eabe85

Browse files
Mark PowersMark Powers
authored andcommitted
Initial commit
0 parents  commit 5eabe85

File tree

113 files changed

+9313
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+9313
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

9781484241783.jpg

27.3 KB
Loading

App_and_AppWindow/Application1.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Application(Gtk.Application):
2+
3+
def __init__(self, *args, **kwargs):
4+
super().__init__(*args, application_id="org.example.myapp",
5+
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
6+
**kwargs)
7+
self.window = None
8+
self.add_main_option("test", ord("t"), GLib.OptionFlags.NONE,
9+
GLib.OptionArg.NONE, "Command line test", None)
10+
11+
def do_startup(self):
12+
Gtk.Application.do_startup(self)
13+
action = Gio.SimpleAction.new("quit", None)
14+
action.connect("activate", self.on_quit)
15+
self.add_action(action)
16+
17+
def do_activate(self):
18+
# We only allow a single window and raise any existing ones
19+
if not self.window:
20+
# Windows are associated with the application
21+
# when the last one is closed the application shuts down
22+
self.window = AppWindow(application=self, title="Main Window")
23+
self.window.present()
24+
25+
def do_command_line(self, command_line):
26+
options = command_line.get_options_dict()
27+
if options.contains("test"):
28+
# This is printed on the main instance
29+
print("Test argument recieved")
30+
self.activate()
31+
return 0

App_and_AppWindow/Application2.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
import gi
5+
gi.require_version('Gtk', '3.0')
6+
from gi.repository import GLib, Gio, Gtk
7+
8+
# This would typically be its own file
9+
MENU_XML="""
10+
<?xml version="1.0" encoding="UTF-8"?>
11+
<interface>
12+
<menu id="app-menu">
13+
<section>
14+
<attribute name="label" translatable="yes">Change label</attribute>
15+
<item>
16+
<attribute name="action">win.change_label</attribute>
17+
<attribute name="target">String 1</attribute>
18+
<attribute name="label" translatable="yes">String 1</attribute>
19+
</item>
20+
<item>
21+
<attribute name="action">win.change_label</attribute>
22+
<attribute name="target">String 2</attribute>
23+
<attribute name="label" translatable="yes">String 2</attribute>
24+
</item>
25+
<item>
26+
<attribute name="action">win.change_label</attribute>
27+
<attribute name="target">String 3</attribute>
28+
<attribute name="label" translatable="yes">String 3</attribute>
29+
</item>
30+
</section>
31+
<section>
32+
<item>
33+
<attribute name="action">win.maximize</attribute>
34+
<attribute name="label" translatable="yes">Maximize</attribute>
35+
</item>
36+
</section>
37+
<section>
38+
<item>
39+
<attribute name="action">app.about</attribute>
40+
<attribute name="label" translatable="yes">_About</attribute>
41+
</item>
42+
<item>
43+
<attribute name="action">app.quit</attribute>
44+
<attribute name="label" translatable="yes">_Quit</attribute>
45+
<attribute name="accel">&lt;Primary&gt;q</attribute>
46+
</item>
47+
</section>
48+
</menu>
49+
</interface>
50+
"""
51+
52+
class AppWindow(Gtk.ApplicationWindow):
53+
54+
def __init__(self, *args, **kwargs):
55+
super().__init__(*args, **kwargs)
56+
# This will be in the windows group and have the "win" prefix
57+
max_action = Gio.SimpleAction.new_stateful("maximize", None,
58+
GLib.Variant.new_boolean(False))
59+
max_action.connect("change-state", self.on_maximize_toggle)
60+
self.add_action(max_action)
61+
# Keep it in sync with the actual state
62+
self.connect("notify::is-maximized",
63+
lambda obj, pspec: max_action.set_state(
64+
GLib.Variant.new_boolean(obj.props.is_maximized)))
65+
lbl_variant = GLib.Variant.new_string("String 1")
66+
lbl_action = Gio.SimpleAction.new_stateful("change_label",
67+
lbl_variant.get_type(),
68+
lbl_variant)
69+
lbl_action.connect("change-state", self.on_change_label_state)
70+
self.add_action(lbl_action)
71+
self.label = Gtk.Label(label=lbl_variant.get_string(),
72+
margin=30)
73+
self.add(self.label)
74+
75+
def on_change_label_state(self, action, value):
76+
action.set_state(value)
77+
self.label.set_text(value.get_string())
78+
79+
def on_maximize_toggle(self, action, value):
80+
action.set_state(value)
81+
if value.get_boolean():
82+
self.maximize()
83+
else:
84+
self.unmaximize()
85+
86+
class Application(Gtk.Application):
87+
88+
def __init__(self, *args, **kwargs):
89+
super().__init__(*args, application_id="org.example.myapp",
90+
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
91+
**kwargs)
92+
self.window = None
93+
self.add_main_option("test", ord("t"), GLib.OptionFlags.NONE,
94+
GLib.OptionArg.NONE, "Command line test", None)
95+
96+
def do_startup(self):
97+
Gtk.Application.do_startup(self)
98+
action = Gio.SimpleAction.new("about", None)
99+
action.connect("activate", self.on_about)
100+
self.add_action(action)
101+
action = Gio.SimpleAction.new("quit", None)
102+
action.connect("activate", self.on_quit)
103+
self.add_action(action)
104+
builder = Gtk.Builder.new_from_string(MENU_XML, -1)
105+
self.set_app_menu(builder.get_object("app-menu"))
106+
107+
def do_activate(self):
108+
# We only allow a single window and raise any existing ones
109+
if not self.window:
110+
# Windows are associated with the application
111+
# when the last one is closed the application shuts down
112+
self.window = AppWindow(application=self, title="Main Window")
113+
self.window.present()
114+
115+
def do_command_line(self, command_line):
116+
options = command_line.get_options_dict()
117+
if options.contains("test"):
118+
# This is printed on the main instance
119+
print("Test argument recieved")
120+
self.activate()
121+
return 0
122+
123+
def on_about(self, action, param):
124+
about_dialog = Gtk.AboutDialog(transient_for=self.window, modal=True)
125+
about_dialog.present()
126+
127+
def on_quit(self, action, param):
128+
self.quit()
129+
130+
131+
if __name__ == "__main__":
132+
app = Application()
133+
app.run(sys.argv)

Basic_Widgets/CheckButtons.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
import gi
5+
gi.require_version('Gtk', '3.0')
6+
from gi.repository import Gtk
7+
8+
class AppWindow(Gtk.ApplicationWindow):
9+
10+
def __init__(self, *args, **kwargs):
11+
super().__init__(*args, **kwargs)
12+
self.set_border_width(10)
13+
check1 = Gtk.CheckButton.new_with_label("I am the main option.")
14+
check2 = Gtk.CheckButton.new_with_label("I rely on the other guy.")
15+
check2.set_sensitive(False)
16+
check1.connect("toggled", self.on_button_checked, check2)
17+
closebutton = Gtk.Button.new_with_mnemonic("_Close")
18+
closebutton.connect("clicked", self.on_button_close_clicked)
19+
vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=0)
20+
vbox.pack_start(check1, False, True, 0)
21+
vbox.pack_start(check2, False, True, 0)
22+
vbox.pack_start(closebutton, False, True, 0)
23+
self.add(vbox)
24+
25+
def on_button_checked(self, check1, check2):
26+
if check1.get_active():
27+
check2.set_sensitive(True);
28+
else:
29+
check2.set_sensitive(False)
30+
31+
def on_button_close_clicked(self, button):
32+
self.destroy()
33+
34+
class Application(Gtk.Application):
35+
36+
def __init__(self, *args, **kwargs):
37+
super().__init__(*args, application_id="org.example.myapp",
38+
**kwargs)
39+
self.window = None
40+
41+
def do_activate(self):
42+
if not self.window:
43+
self.window = AppWindow(application=self, title="Check Buttons")
44+
self.window.show_all()
45+
self.window.present()
46+
47+
if __name__ == "__main__":
48+
app = Application()
49+
app.run(sys.argv)

Basic_Widgets/Exercise_1.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
import gi
5+
gi.require_version('Gtk', '3.0')
6+
from gi.repository import Gtk
7+
import os
8+
from pathlib import Path
9+
10+
class AppWindow(Gtk.ApplicationWindow):
11+
12+
def __init__(self, *args, **kwargs):
13+
super().__init__(*args, **kwargs)
14+
self.set_border_width(10)
15+
16+
rnm = Gtk.Button.new_with_label("Apply")
17+
name = Gtk.Entry.new()
18+
rnm.set_sensitive(False)
19+
name.set_sensitive(False)
20+
21+
file = Gtk.FileChooserButton("Choose File", Gtk.FileChooserAction.OPEN)
22+
file.set_current_folder(str(Path.home()))
23+
24+
file.connect("selection-changed", self.on_file_changed, file, rnm,
25+
name)
26+
rnm.connect("clicked", self.on_rename_clicked, file, rnm, name)
27+
28+
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
29+
hbox.pack_start(name, True, True, 0)
30+
hbox.pack_start(rnm, False, True, 0)
31+
32+
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
33+
vbox.pack_start(file, False, True, 0)
34+
vbox.pack_start(hbox, False, True, 0)
35+
36+
self.add(vbox)
37+
38+
39+
def on_file_changed(self, chooser, file, rnm, name):
40+
fn = file.get_filename()
41+
mode = os.access(fn, os.W_OK)
42+
43+
rnm.set_sensitive(mode)
44+
name.set_sensitive(mode)
45+
46+
def on_rename_clicked(self, chooser, file, rnm, name):
47+
old = file.get_filename()
48+
location = file.get_current_folder()
49+
new = location + "/" + name.get_text()
50+
51+
os.rename(old, new)
52+
rnm.set_sensitive(False)
53+
name.set_sensitive(False)
54+
55+
class Application(Gtk.Application):
56+
57+
def __init__(self, *args, **kwargs):
58+
super().__init__(*args, application_id="org.example.myapp",
59+
**kwargs)
60+
self.window = None
61+
62+
def do_activate(self):
63+
if not self.window:
64+
self.window = AppWindow(application=self, title="File Chooser Button Exercise")
65+
self.window.show_all()
66+
self.window.present()
67+
68+
if __name__ == "__main__":
69+
app = Application()
70+
app.run(sys.argv)

Basic_Widgets/Exercise_2.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
import gi
5+
gi.require_version('Gtk', '3.0')
6+
from gi.repository import Gtk
7+
import os
8+
from pathlib import Path
9+
10+
class AppWindow(Gtk.ApplicationWindow):
11+
12+
def __init__(self, *args, **kwargs):
13+
super().__init__(*args, **kwargs)
14+
self.set_border_width(10)
15+
16+
adj1 = Gtk.Adjustment.new(0.5, 0.0, 1.0, 0.01, 0.02, 0.02)
17+
adj2 = Gtk.Adjustment.new(0.5, 0.0, 1.02, 0.01, 0.02, 0.02)
18+
19+
spin = Gtk.SpinButton.new(adj1, 0.01, 2)
20+
scale = Gtk.Scale.new(Gtk.Orientation.HORIZONTAL, adj2)
21+
check = Gtk.CheckButton.new_with_label("Synchronize Spin and Scale")
22+
23+
check.set_active(True)
24+
scale.set_digits(2)
25+
scale.set_value_pos(Gtk.PositionType.RIGHT)
26+
27+
spin.connect("value_changed", self.on_spin_value_changed, spin, scale, check)
28+
scale.connect("value_changed", self.on_scale_value_changed, spin, scale, check)
29+
30+
vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 5);
31+
vbox.pack_start(spin, False, True, 0)
32+
vbox.pack_start(scale, False, True, 0);
33+
vbox.pack_start(check, False, True, 0);
34+
35+
self.add(vbox)
36+
37+
38+
def on_spin_value_changed(self, widget, spin, scale, check):
39+
val1 = spin.get_value()
40+
val2 = scale.get_value()
41+
42+
if (check.get_active() and val1 != val2):
43+
if isinstance(widget, Gtk.SpinButton):
44+
scale.set_value(val1)
45+
else:
46+
spin.set_value(val2)
47+
48+
49+
50+
class Application(Gtk.Application):
51+
52+
def __init__(self, *args, **kwargs):
53+
super().__init__(*args, application_id="org.example.myapp",
54+
**kwargs)
55+
self.window = None
56+
57+
def do_activate(self):
58+
if not self.window:
59+
self.window = AppWindow(application=self, title="Exercise 2")
60+
self.window.show_all()
61+
self.window.present()
62+
63+
if __name__ == "__main__":
64+
app = Application()
65+
app.run(sys.argv)

0 commit comments

Comments
 (0)