{"id":505,"date":"2020-05-06T00:08:26","date_gmt":"2020-05-06T07:08:26","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=505"},"modified":"2021-10-27T12:03:57","modified_gmt":"2021-10-27T19:03:57","slug":"creating-a-better-version-of-python-3-tkinter-hello-world","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/creating-a-better-version-of-python-3-tkinter-hello-world\/","title":{"rendered":"#75 Creating a better version of Python 3 Tkinter Hello World"},"content":{"rendered":"<p>Creating a better version of Python 3 Tkinter Hello World<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport tkinter as tk\r\nfrom tkinter import ttk\r\n\r\n\r\nclass HelloView(tk.Frame):\r\n    &quot;&quot;&quot;A friendly little module&quot;&quot;&quot;\r\n\r\n    def __init__(self, parent, *args, **kwargs):\r\n        super().__init__(parent, *args, **kwargs)\r\n\r\n        self.name = tk.StringVar()\r\n        self.hello_string = tk.StringVar()\r\n        self.hello_string.set(&quot;Hello World&quot;)\r\n\r\n        name_label = ttk.Label(self, text=&quot;Name:&quot;)\r\n        name_entry = ttk.Entry(self, textvariable=self.name)\r\n        ch_button = ttk.Button(self, text=&quot;Change&quot;, command=self.on_change)\r\n        hello_label = ttk.Label(self, textvariable=self.hello_string,\r\n            font=(&quot;TkDefaultFont&quot;, 64), wraplength=400)\r\n\r\n        # Layout form\r\n        name_label.grid(row=0, column=0, sticky=tk.W)\r\n        name_entry.grid(row=0, column=1, sticky=(tk.W + tk.E))\r\n        ch_button.grid(row=0, column=2, sticky=tk.E)\r\n        hello_label.grid(row=1, column=0, columnspan=3)\r\n        self.columnconfigure(1, weight=1)\r\n\r\n    def on_change(self):\r\n        &quot;&quot;&quot;Handle Change button clicks&quot;&quot;&quot;\r\n        if self.name.get().strip():\r\n            self.hello_string.set(&quot;Hello &quot; + self.name.get())\r\n        else:\r\n            self.hello_string.set(&quot;Hello World&quot;)\r\n\r\n\r\nclass MyApplication(tk.Tk):\r\n    &quot;&quot;&quot;Hello World Main Application&quot;&quot;&quot;\r\n\r\n    def __init__(self, *args, **kwargs):\r\n        super().__init__(*args, **kwargs)\r\n\r\n        # set the window properties\r\n        self.title(&quot;Hello Tkinter&quot;)\r\n        self.geometry(&quot;400x300&quot;)\r\n        self.resizable(width=False, height=False)\r\n\r\n        # Define the UI\r\n        HelloView(self).grid(sticky=(tk.E + tk.W + tk.N + tk.S))\r\n        self.columnconfigure(0, weight=1)\r\n\r\n\r\nif __name__ == '__main__':\r\n    app = MyApplication()\r\n    app.mainloop()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/2020-05-06_000413.png?resize=402%2C332\" alt=\"\" width=\"402\" height=\"332\" class=\"alignnone size-full wp-image-506\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/2020-05-06_000413.png?w=402&amp;ssl=1 402w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/2020-05-06_000413.png?resize=300%2C248&amp;ssl=1 300w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a better version of Python 3 Tkinter Hello World import tkinter as tk from tkinter import ttk class HelloView(tk.Frame): &quot;&quot;&quot;A friendly little module&quot;&quot;&quot; def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.name = tk.StringVar() self.hello_string = tk.StringVar() self.hello_string.set(&quot;Hello World&quot;) name_label = ttk.Label(self, text=&quot;Name:&quot;) name_entry = ttk.Entry(self, textvariable=self.name) ch_button = ttk.Button(self, text=&quot;Change&quot;, command=self.on_change) hello_label = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2,26],"tags":[],"class_list":["post-505","post","type-post","status-publish","format-standard","hentry","category-python","category-tkinter"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-89","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":575,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/06\/83-application-saving-the-data-to-csv-file\/","url_meta":{"origin":505,"position":0},"title":"#83 Application, Saving the data to csv file","author":"gantovnik","date":"2020-06-13","format":false,"excerpt":"[code language=\"python\"] from datetime import datetime import os import csv import tkinter as tk from tkinter import ttk class LabelInput(tk.Frame): \"\"\"A widget containing a label and input together.\"\"\" def __init__(self, parent, label='', input_class=ttk.Entry, input_var=None, input_args=None, label_args=None, **kwargs): super().__init__(parent, **kwargs) input_args = input_args or {} label_args = label_args or {} self.variable\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/06\/2020-06-13_025234.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/06\/2020-06-13_025234.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/06\/2020-06-13_025234.jpg?resize=525%2C300 1.5x"},"classes":[]},{"id":1025,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/182-feet-to-meter-converter-using-python-tkinter\/","url_meta":{"origin":505,"position":1},"title":"#182 Feet to meter converter using python tkinter","author":"gantovnik","date":"2021-11-04","format":false,"excerpt":"#182 Feet to meter converter using python tkinter [code language=\"python\"] from tkinter import * from tkinter import ttk def calculate(*args): try: value = float(feet.get()) meters.set(int(0.3048 * value * 10000.0 + 0.5)\/10000.0) except ValueError: pass root = Tk() root.title(\"Feet to meters converter\") mainframe = ttk.Frame(root, padding=\"3 3 12 12\") mainframe.grid(column=0, row=0,\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex182.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":512,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/the-entry-widget\/","url_meta":{"origin":505,"position":2},"title":"#77 The Entry widget","author":"gantovnik","date":"2020-05-06","format":false,"excerpt":"The Entry widget [code language=\"python\"] import tkinter as tk from tkinter import ttk parent = tk.Tk() my_text_var = tk.StringVar() my_entry = ttk.Entry( parent, textvariable=my_text_var ) my_entry.pack() parent.mainloop() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/2020-05-06_014229.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":567,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/06\/82\/","url_meta":{"origin":505,"position":3},"title":"#82 The Button widget","author":"gantovnik","date":"2020-06-13","format":false,"excerpt":"[code language=\"python\"] import tkinter as tk from tkinter import ttk parent = tk.Tk() tvar = tk.StringVar() def swaptext(): tvar.set('There' if tvar.get() == 'Hi' else 'Hi') my_button = ttk.Button(parent, textvariable=tvar, command=swaptext) my_button.pack() parent.mainloop() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/06\/2020-06-13_010517.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":519,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/the-combobox-widget\/","url_meta":{"origin":505,"position":4},"title":"#79 The Combobox widget","author":"gantovnik","date":"2020-05-06","format":false,"excerpt":"The Combobox widget [code language=\"python\"] import tkinter as tk from tkinter import ttk parent = tk.Tk() my_string_var = tk.StringVar() combobox = ttk.Combobox( parent, textvariable=my_string_var, values=[\"Option 1\", \"Option 2\", \"Option 3\"] ) combobox.pack() parent.mainloop() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/2020-05-06_015402.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":508,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/the-button-widget\/","url_meta":{"origin":505,"position":5},"title":"#76 The Button widget","author":"gantovnik","date":"2020-05-06","format":false,"excerpt":"The Button widget [code language=\"python\"] import tkinter as tk from tkinter import ttk parent = tk.Tk() tvar = tk.StringVar() def swaptext(): tvar.set('There' if tvar.get() == 'Hi' else 'Hi') my_button = ttk.Button(parent, textvariable=tvar, command=swaptext) my_button.pack() parent.mainloop() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/2020-05-06_012455.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=505"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/505\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}