

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tkinter &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/tkinter/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Tue, 08 Aug 2023 07:06:09 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>tkinter &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How can i change tab with button in tkinter?</title>
		<link>https://stackoverflow.max-everyday.com/2023/08/how-can-i-change-tab-with-button-in-tkinter/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/08/how-can-i-change-tab-with-button-in-tkinter/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Tue, 08 Aug 2023 07:06:08 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4980</guid>

					<description><![CDATA[在有tab 的情況下, 如果有必填的欄位使用者有...]]></description>
										<content:encoded><![CDATA[
<p>在有tab 的情況下, 如果有必填的欄位使用者有少打時, 遇到錯誤要先切換到該分頁, 如果只有讓某一個控制項取得focuse 是沒用的, 需要額外讓 tab <code>.select()</code>:</p>



<p>解法:<br><a href="https://stackoverflow.com/questions/61691042/how-can-i-change-tab-with-button-in-tkinter">https://stackoverflow.com/questions/61691042/how-can-i-change-tab-with-button-in-tkinter</a></p>



<p>範例:</p>



<p>Choose a&nbsp;<code>ttk.Notebook</code>&nbsp;instance to use&nbsp;<code>select(tab_id)</code>. In your code,Use</p>



<pre class="wp-block-code"><code>Tabs.select(tab2)</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>要讓輸入的元件focus 設使用 <code>.focus_set()</code>:<br><a href="https://stackoverflow.com/questions/70191953/is-there-a-function-to-switch-tab-and-enter-button-in-tkinter">https://stackoverflow.com/questions/70191953/is-there-a-function-to-switch-tab-and-enter-button-in-tkinter</a></p>



<p>You can bind the&nbsp;<code>&lt;Return&gt;</code>&nbsp;key event on the&nbsp;<code>Entry</code>&nbsp;widget. Then in the bind callback, get the next widget in the focus order by&nbsp;<code>.tk_focusNext()</code>&nbsp;and move focus to this widget:</p>



<pre class="wp-block-code"><code>import tkinter as tk

root = tk.Tk()

for i in range(10):
    e = tk.Entry(root)
    e.grid(row=0, column=i)
    e.bind('&lt;Return&gt;', lambda e: e.widget.tk_focusNext().focus_set())

root.mainloop()</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/08/how-can-i-change-tab-with-button-in-tkinter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Change width of dropdown listbox of a ttk combobox</title>
		<link>https://stackoverflow.max-everyday.com/2023/05/change-width-of-dropdown-listbox-of-a-ttk-combobox/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/05/change-width-of-dropdown-listbox-of-a-ttk-combobox/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Thu, 25 May 2023 02:44:58 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4854</guid>

					<description><![CDATA[tkinter 的combobox 設定 wid...]]></description>
										<content:encoded><![CDATA[
<p>tkinter 的combobox 設定 width 來調整寬度, 解法1:<br><a href="https://stackoverflow.com/questions/53785794/is-there-a-way-to-resize-the-combobox-entry-size">https://stackoverflow.com/questions/53785794/is-there-a-way-to-resize-the-combobox-entry-size</a></p>



<p><code>Combobox</code>&nbsp;has a&nbsp;<strong>width</strong>&nbsp;attribute which lets you control its size. The&nbsp;<code>width</code>&nbsp;is in terms of the number of characters. So for example, if you know that your combobox entries are single digit numbers, you can set the width attribute as, say 1. Here is an example.</p>



<pre class="wp-block-code"><code>import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
tList = ttk.Combobox(root, values=&#91;1, 2, 3, 4, 5], state="readonly", width=1)
tList.current(0)
tList.grid(row=0, column=1, padx=10, pady=10)
root.mainloop()</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>解法2: 只調整彈出的視窗<br><a href="https://stackoverflow.com/questions/39915275/change-width-of-dropdown-listbox-of-a-ttk-combobox">https://stackoverflow.com/questions/39915275/change-width-of-dropdown-listbox-of-a-ttk-combobox</a></p>



<pre class="wp-block-code"><code>import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkfont

def on_combo_configure(event):
    global fruit
    font = tkfont.nametofont(str(event.widget.cget('font')))
    width = font.measure(fruit&#91;0] + "0") - event.width
    style = ttk.Style()
    style.configure('TCombobox', postoffset=(0,0,width,0))

root = tk.Tk()
root.title("testing the combobox")
root.geometry('300x300+50+50')
fruit = &#91;'apples are the best', 'bananas are better']

c = ttk.Combobox(root, values=fruit, width=10)
c.bind('&lt;Configure&gt;', on_combo_configure)
c.pack()

root.mainloop()
</code></pre>



<p>We do this using the event binding as this ensures we can get the actual size of the widget on screen to remove that width from the offset.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/05/change-width-of-dropdown-listbox-of-a-ttk-combobox/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>tkinter open file dialog</title>
		<link>https://stackoverflow.max-everyday.com/2023/03/tkinter-open-file-dialog/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/03/tkinter-open-file-dialog/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Sun, 05 Mar 2023 07:32:19 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4615</guid>

					<description><![CDATA[最簡易範例，不實用： 實用的簡易範例： 多選範例...]]></description>
										<content:encoded><![CDATA[
<p>最簡易範例，不實用：</p>



<pre class="wp-block-code"><code>import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)</code></pre>



<p></p>



<p>實用的簡易範例：</p>



<pre class="wp-block-code"><code>import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo

# create the root window
root = tk.Tk()
root.title('Tkinter Open File Dialog')
root.resizable(False, False)
root.geometry('300x150')


def select_file():
    filetypes = (
        ('text files', '*.txt'),
        ('All files', '*.*')
    )

    filename = fd.askopenfilename(
        title='Open a file',
        initialdir='/',
        filetypes=filetypes)

    showinfo(
        title='Selected File',
        message=filename
    )


# open button
open_button = ttk.Button(
    root,
    text='Open a File',
    command=select_file
)

open_button.pack(expand=True)


# run the application
root.mainloop()</code></pre>



<p></p>



<p>多選範例：</p>



<pre class="wp-block-code"><code>import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo

# create the root window
root = tk.Tk()
root.title('Tkinter File Dialog')
root.resizable(False, False)
root.geometry('300x150')


def select_files():
    filetypes = (
        ('text files', '*.txt'),
        ('All files', '*.*')
    )

    filenames = fd.askopenfilenames(
        title='Open files',
        initialdir='/',
        filetypes=filetypes)

    showinfo(
        title='Selected Files',
        message=filenames
    )


# open button
open_button = ttk.Button(
    root,
    text='Open Files',
    command=select_files
)

open_button.pack(expand=True)

root.mainloop()</code></pre>



<p>the askopenfiles() function shows a file dialog and returns file objects of the selected files:</p>



<pre class="wp-block-code"><code>f = fd.askopenfiles()</code></pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/03/tkinter-open-file-dialog/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Save File Dialog Box in Tkinter</title>
		<link>https://stackoverflow.max-everyday.com/2023/03/save-file-dialog-box-in-tkinter/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/03/save-file-dialog-box-in-tkinter/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Sun, 05 Mar 2023 07:12:41 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4613</guid>

					<description><![CDATA[在python 下的 tkinter 叫出 sa...]]></description>
										<content:encoded><![CDATA[
<p>在python 下的 tkinter 叫出 save as 對話框，取得要存檔的名稱。</p>



<pre class="wp-block-code"><code>from tkinter import *
from tkinter.filedialog import asksaveasfile

win= Tk()
win.geometry("750x250")
def save_file():
   f = asksaveasfile(initialfile = 'Untitled.txt',
defaultextension=".txt",filetypes=&#91;("All Files","*.*"),("Text Documents","*.txt")])

btn= Button(win, text= "Save", command= lambda:save_file())
btn.pack(pady=10)

win.mainloop()</code></pre>



<p>使用起來還滿簡單的。</p>



<p></p>



<p>範例2:</p>



<pre class="wp-block-code"><code>from tkinter import *
from tkinter import filedialog
#setting up parent window
root = Tk()

#function definition for opening file dialog
def savef():
    myFile = filedialog.asksaveasfile(mode='w', defaultextension='.txt')
    if myFile is None:
        return
    data = teditor.get(1.0,'end')
    myFile.write(data)
    myFile.close()</code></pre>



<p>說明：<strong>asksaveasfile</strong> 會取得 <strong>file</strong>, <strong>asksaveasfilename</strong>  才是取得 <strong>string</strong>.</p>



<p></p>



<p>範例3:</p>



<pre class="wp-block-code"><code>from tkinter.filedialog import asksaveasfile
from tkinter import *

base = Tk()

def SaveFile():
   data = &#91;('All tyes(*.*)', '*.*'),("csv file(*.csv)","*.csv")]
   file = asksaveasfilename(filetypes = data, defaultextension = data)
   # file will have file name provided by user.
   # Now we can use this file name to save file.
   with open(file,"w") as f:
      f.write(&lt;data you want to save>)

save_btn = Button(base, text = 'Click to save file ', command = SaveFile)
save_btn.pack(side = TOP, pady = 20,padx = 50)

base.mainloop()</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/03/save-file-dialog-box-in-tkinter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>tkinter show error: macOS 11 or later required</title>
		<link>https://stackoverflow.max-everyday.com/2020/12/tkinter-show-error-macos-11-or-later-required/</link>
					<comments>https://stackoverflow.max-everyday.com/2020/12/tkinter-show-error-macos-11-or-later-required/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 23 Dec 2020 17:03:34 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[macOS]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=3646</guid>

					<description><![CDATA[作業系統是 macOS "Big Sur", T...]]></description>
										<content:encoded><![CDATA[
<pre id="block-3cbf8df6-567c-4efc-b697-bd2c9cf4cca8" class="wp-block-preformatted">作業系統是 macOS "Big Sur", This is the code:</pre>



<pre id="block-81379f04-36aa-447a-b275-7bb827256e4e" class="wp-block-preformatted"><code>from tkinter import * </code>
<code>window = Tk() </code>
<code>window.mainloop()</code></pre>



<hr class="wp-block-separator"/>



<p id="block-3cbf8df6-567c-4efc-b697-bd2c9cf4cca8">error message:</p>



<pre class="wp-block-preformatted">macOS 11 or later required</pre>



<hr class="wp-block-separator"/>



<p>runtime screen:</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="763" height="480" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/12/131669037_892507361490221_4292080155450641838_n.png" alt="" class="wp-image-3648" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/12/131669037_892507361490221_4292080155450641838_n.png?v=1608742461 763w, https://stackoverflow.max-everyday.com/wp-content/uploads/2020/12/131669037_892507361490221_4292080155450641838_n-600x377.png?v=1608742461 600w" sizes="(max-width: 763px) 100vw, 763px" /></figure>



<hr class="wp-block-separator"/>



<p>網傳解法：</p>



<p>This is an issue in the way&nbsp;<code>brew</code>&nbsp;installs Python (<a href="https://bugs.python.org/issue42480">source</a>). If you install Python directly via the official installer&nbsp;<a href="https://www.python.org/downloads/mac-osx/">here</a>&nbsp;then&nbsp;<code>tkinter</code>&nbsp;should work as expected.</p>



<p>重新下載官方的Python，下載用網址：<br><a href="https://www.python.org/downloads/">https://www.python.org/downloads/</a></p>



<p>會下載檔案：python-3.x.x-macosx1x.x.pkg, 點2下即可開始安裝 python為新的版本。</p>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">相關網頁</h2>



<p>Interpreter crashes trying to use tkinter library<br><a href="https://stackoverflow.com/questions/65315077/interpreter-crashes-trying-to-use-tkinter-library">https://stackoverflow.com/questions/65315077/interpreter-crashes-trying-to-use-tkinter-library</a></p>



<p>Python Tkinter crashes on macOS 11.1 beta<br><a href="https://bugs.python.org/issue42480">https://bugs.python.org/issue42480</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2020/12/tkinter-show-error-macos-11-or-later-required/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>a Tkinter GUI stop button to break an infinite loop?</title>
		<link>https://stackoverflow.max-everyday.com/2019/04/a-tkinter-gui-stop-button-to-break-an-infinite-loop/</link>
					<comments>https://stackoverflow.max-everyday.com/2019/04/a-tkinter-gui-stop-button-to-break-an-infinite-loop/#comments</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 24 Apr 2019 15:16:20 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=2754</guid>

					<description><![CDATA[有人想用tkinter寫按鈕A，按鈕A事件中有w...]]></description>
										<content:encoded><![CDATA[
<p>有人想用tkinter寫按鈕A，按鈕A事件中有while迴圈，造成按下按鈕A，就無法再去按下按鈕B。求助如何可以讓按鈕A持續跑while，但是滑鼠也可以去按下按鈕B‧</p>



<p>這題的解法，開新的 thread 就解決了。</p>



<hr class="wp-block-separator"/>



<p>如果題目換成，按下按鈕A，中斷一個在其他地方執行的無窮迴圈。<br><a href="https://stackoverflow.com/questions/27050492/how-do-you-create-a-tkinter-gui-stop-button-to-break-an-infinite-loop">https://stackoverflow.com/questions/27050492/how-do-you-create-a-tkinter-gui-stop-button-to-break-an-infinite-loop</a></p>



<h4 class="wp-block-heading">解法1：使用 .after()</h4>



<p>You cannot start a&nbsp;<code>while True:</code>&nbsp;loop in the same thread that the Tkinter event loop is operating in. Doing so will block Tkinter&#8217;s loop and cause the program to freeze.</p>



<p>For a simple solution, you could use&nbsp;<a href="http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method"><code>Tk.after</code></a>&nbsp;to run a process in the background every second or so. Below is a script to demonstrate:</p>



<pre class="wp-block-code"><code>from Tkinter import *

running = True  # Global flag

def scanning():
    if running:  # Only do this if the Stop button has not been clicked
        print "hello"

    # After 1 second, call scanning again (create a recursive loop)
    root.after(1000, scanning)

def start():
    """Enable scanning by setting the global flag to True."""
    global running
    running = True

def stop():
    """Stop scanning by setting the global flag to False."""
    global running
    running = False

root = Tk()
root.title("Title")
root.geometry("500x500")

app = Frame(root)
app.grid()

start = Button(app, text="Start Scan", command=start)
stop = Button(app, text="Stop", command=stop)

start.grid()
stop.grid()

root.after(1000, scanning)  # After 1 second, call scanning
root.mainloop()</code></pre>



<p>Of course, you may want to refactor this code into a class and have <code>running</code> be an attribute of it. Also, if your program becomes anything complex, it would be beneficial to look into Python&#8217;s <a href="https://docs.python.org/2/library/threading.html"><code>threading</code> module</a> so that your <code>scanning</code> function can be executed in a separate thread.</p>



<hr class="wp-block-separator"/>



<h4 class="wp-block-heading">解法2：global</h4>



<p>Here is a different solution, with the following advantages:</p>



<ol class="wp-block-list"><li>Does not require manually creating separate threads</li><li>Does not use&nbsp;<code>Tk.after</code>&nbsp;calls. Instead, the original style of code with a continuous loop is preserved. The main advantage of this is that you do not have to manually specify a number of milliseconds that determines how often your code inside the loop runs, it simply gets to run as often as your hardware allows.</li></ol>



<p><strong>Note:</strong>&nbsp;I&#8217;ve only tried this with&nbsp;<strong>python 3</strong>, not with python 2. I suppose the same should work in python 2 too, I just don&#8217;t know 100% for sure.</p>



<p>For the UI code and start/stopping logic, I&#8217;ll use mostly the same code as in iCodez&#8217; answer. An important difference is that I assume we&#8217;ll always have a loop running, but decide within that loop what to do based on which buttons have been pressed recently:</p>



<pre class="wp-block-code"><code>from tkinter import *

running = True  # Global flag
idx = 0  # loop index

def start():
    """Enable scanning by setting the global flag to True."""
    global running
    running = True

def stop():
    """Stop scanning by setting the global flag to False."""
    global running
    running = False

root = Tk()
root.title("Title")
root.geometry("500x500")

app = Frame(root)
app.grid()

start = Button(app, text="Start Scan", command=start)
stop = Button(app, text="Stop", command=stop)

start.grid()
stop.grid()

while True:
    if idx % 500 == 0:
        root.update()

    if running:
        print("hello")
        idx += 1</code></pre>



<p>In this code, we do not call&nbsp;<code>root.mainloop()</code>&nbsp;to have the tkinter GUI continually updating. Instead, we manually update it every so often (in this case, every 500 loop iterations).</p>



<p>Theoretically, this means we may not instantly stop the loop as soon as we hit the Stop button. For example, if at the exact moment where we hit the Stop button, we&#8217;re at iteration 501, this code will continue looping until iteration 1000 has been hit. So, the disadvantage of this code is that we have a slighlty less responsive GUI in theory (but it will be unnoticeable if the code within your loop is fast). In return, we get the code inside the loop to run almost as fast as possible (only with sometimes overhead from a GUI&nbsp;<code>update()</code>&nbsp;call), and have it running inside the main thread.</p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2019/04/a-tkinter-gui-stop-button-to-break-an-infinite-loop/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>[Python] tkinter changing the text on a label</title>
		<link>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-changing-the-text-on-a-label/</link>
					<comments>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-changing-the-text-on-a-label/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Mon, 08 Apr 2019 18:03:48 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=2701</guid>

					<description><![CDATA[Above sentence make labe...]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-code"><code>self.labelText = 'change the value'</code></pre>



<p>Above sentence make labelText to reference &#8216;change the value&#8217;, but not change depositLabel&#8217;s text.</p>



<p>To change depositLabel&#8217;s text, use one of following setences:</p>



<pre class="wp-block-code"><code>self.depositLabel['text'] = 'change the value'</code></pre>



<p>OR</p>



<pre class="wp-block-code"><code>self.depositLabel.config(text='change the value')</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-changing-the-text-on-a-label/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[Python] tkinter Combobox disable typing</title>
		<link>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-combobox-disable-typing/</link>
					<comments>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-combobox-disable-typing/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Mon, 08 Apr 2019 17:41:25 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=2698</guid>

					<description><![CDATA[default combo 是可以輸入的，問題會...]]></description>
										<content:encoded><![CDATA[
<p>default combo 是可以輸入的，問題會不是預期的文字內容，而且也無法使用 bind(&#8216;&lt;>&#8217;) 去處理使用者輸入的內容。解法：</p>



<p>You can set the&nbsp;<code>state</code>&nbsp;to&nbsp;<code>"readonly"</code></p>



<pre class="wp-block-code"><code>cb = ttk.Combobox(root, state="readonly", 
                  values=("one", "two", "three"))</code></pre>



<p>From the&nbsp;<a href="https://docs.python.org/3.6/library/tkinter.ttk.html#options">python 3.6 documentation</a>:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong>state</strong>: One of “normal”, “readonly”, or “disabled”. In the “readonly” state, the value may not be edited directly, and the user can only selection of the values from the dropdown list. In the “normal” state, the text field is directly editable. In the “disabled” state, no interaction is possible.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-combobox-disable-typing/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[Python] tkinter Combobox onchange</title>
		<link>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-combobox-onchange/</link>
					<comments>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-combobox-onchange/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Thu, 04 Apr 2019 12:44:48 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=2685</guid>

					<description><![CDATA[要存取 combobox 的 onselecte...]]></description>
										<content:encoded><![CDATA[
<p>要存取 combobox 的 onselected 事件，請使用下面範例：</p>



<p>bind &lt;> to a method&#8230;</p>



<pre class="wp-block-code"><code>import tkinter as tk
from tkinter import ttk

class Main(tk.Tk):

  def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    self.container = tk.Frame(self)
    self.container.pack(side="top", fill = "both", expand=True)
    self.container.grid_rowconfigure(0, weight=1)
    self.container.grid_columnconfigure(0, weight=1)
    self.cb=ttk.Combobox(self.container, values=[0,1, 2, 3] , state='readonly')
    self.cb.bind('&lt;&lt;ComboboxSelected>>', self.modified)    
    self.cb.pack()

  def modified (self, event) :
      print(self.cb.get())

main = Main()
main.mainloop()</code></pre>



<hr class="wp-block-separator"/>



<p>範例2號：</p>



<pre class="wp-block-code"><code>import tkinter as tk
from tkinter import ttk

def callbackFunc(event):
     print("New Element Selected")
     
app = tk.Tk() 
app.geometry('200x100')

labelTop = tk.Label(app,
                    text = "Choose your favourite month")
labelTop.grid(column=0, row=0)

comboExample = ttk.Combobox(app, 
                            values=[
                                    "January", 
                                    "February",
                                    "March",
                                    "April"])


comboExample.grid(column=0, row=1)
comboExample.current(1)

comboExample.bind("&lt;&lt;ComboboxSelected>>", callbackFunc)


app.mainloop()</code></pre>



<p>範例2號的資料來源：<br><a href="https://github.com/DelftStack/DelftStack/blob/master/Tutorial/Tkinter/Tkinter%20Combobox_Virtual%20Event%20Binding.py">https://github.com/DelftStack/DelftStack/blob/master/Tutorial/Tkinter/Tkinter%20Combobox_Virtual%20Event%20Binding.py</a><br><br></p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2019/04/python-tkinter-combobox-onchange/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[Python] How to line left justify label and entry boxes in Tkinter grid</title>
		<link>https://stackoverflow.max-everyday.com/2018/12/python-how-to-line-left-justify-label-and-entry-boxes-in-tkinter-grid/</link>
					<comments>https://stackoverflow.max-everyday.com/2018/12/python-how-to-line-left-justify-label-and-entry-boxes-in-tkinter-grid/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 14 Dec 2018 17:33:53 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[電腦相關應用]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tkinter]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=2541</guid>

					<description><![CDATA[在 tkinter 裡 grid 區塊如何對齊的...]]></description>
										<content:encoded><![CDATA[
<p>在 tkinter 裡 grid 區塊如何對齊的方式。直接調整 .grid() 裡的屬性 sticky = W, sticky = E, sticky = N。我試了發現 ：</p>



<ul class="wp-block-list"><li>W = LEFT, </li><li>E = RIGHT</li><li>N = Center, default 值是 N. </li></ul>



<hr class="wp-block-separator"/>



<p>Normally to accomplish what you are after you would simply put sticky = W in the grid options for the Entry widget to left-justify the contents of the cell. However, this will only work for each individual Frame, leaving the contents of each separate LabeledFrame out of alignment.</p>



<p>Easiest way to fix this without changing much code:</p>



<p>You&#8217;ll want to add a line to your for loop. If you specify a large minimum-width of the column that self.field&#8217;s Frame is inserted into, you can be sure that things will align how you want them to. I&#8217;ve also added config options to the grid calls within the LabeledEntry class: sticky = W for the Label &amp; sticky = E for the Entry.</p>



<hr class="wp-block-separator"/>



<p>使用 .grid() 可以把元件加入到 layout 裡, 使用 .grid_forget() 可以 hide。<br><br>Tkinter: how to show and hide Frame class:</p>



<p><code>f</code>&nbsp;will be a&nbsp;<em>class</em>, not an&nbsp;<em>instance</em>. Since&nbsp;<code>self.frames</code>&nbsp;is a dictionary with the actual frames being the values, you want to loop over the values:</p>



<pre class="wp-block-code"><code>for f in self.frames.values():
    f.grid_forget()</code></pre>



<hr class="wp-block-separator"/>



<p>在套用 UI 時，還有一些參數可以使用，例如：padx、pady</p>



<pre class="wp-block-preformatted"><strong>config(**options)</strong><br>Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.<br><em>**options</em><br>Widget options.<br><em>background=</em><br>The background color to use in this frame. This defaults to the application background color. To prevent updates, set the color to an empty string. (the option database name is background, the class is Background)<br><em>bg=</em><br>Same as&nbsp;<strong>background</strong>.<br><em>borderwidth=</em><br>Border width. Defaults to 0 (no border). (borderWidth/BorderWidth)<br><em>bd=</em><br>Same as&nbsp;<strong>borderwidth</strong>.<br><em>class=</em><br>Default is Frame. (class/Class)<br><em>colormap=</em><br>Some displays support only 256 colors (some use even less). Such displays usually provide a color map to specify which 256 colors to use. This option allows you to specify which color map to use for this frame, and its child widgets.&nbsp;<br>By default, a new frame uses the same color map as its parent. Using this option, you can reuse the color map of another window instead (this window must be on the same screen and have the same visual characteristics). You can also use the value “new” to allocate a new color map for this frame.&nbsp;<br>You cannot change this option once you’ve created the frame. (colormap/Colormap)<br><em>container=</em><br>Default is 0. (container/Container)<br><em>cursor=</em><br>The cursor to show when the mouse pointer is placed over this widget. Default is a system specific arrow cursor. (cursor/Cursor)<br><em>height=</em><br>Default is 0. (height/Height)<br><em>highlightbackground=</em><br>Default is system specific. (highlightBackground/HighlightBackground)<br><em>highlightcolor=</em><br>Default value is system specific. (highlightColor/HighlightColor)<br><em>highlightthickness=</em><br>Default is 0. (highlightThickness/HighlightThickness)<br><em>padx=</em><br>Horizontal padding. Default is 0. (padX/Pad)<br><em>pady=</em><br>Vertical padding. Default is 0. (padY/Pad)<br><em>relief=</em><br>Border decoration. The default is&nbsp;<strong>FLAT</strong>. Other possible values are&nbsp;<strong>SUNKEN</strong>,&nbsp;<strong>RAISED</strong>,&nbsp;<strong>GROOVE</strong>, and&nbsp;<strong>RIDGE</strong>.&nbsp;<br>Note that to show the border, you need to change the<strong>borderwidth</strong>&nbsp;from its default value of 0. (relief/Relief)<br><em>takefocus=</em><br>If true, the user can use the&nbsp;<strong>Tab</strong>&nbsp;key to move to this widget. The default value is 0. (takeFocus/TakeFocus)<br><em>visual=</em><br>No default value. (visual/Visual)<br><em>width=</em><br>Default value is 0. (width/Width)</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2018/12/python-how-to-line-left-justify-label-and-entry-boxes-in-tkinter-grid/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
