EscapeキーをバインドしてTkinterのウィンドウを閉じる方法は?
Tkinterイベントは、アプリケーションをインタラクティブで機能的にするために非常に役立ちます。これは、アプリケーションの内部機能と対話する方法を提供し、ClickまたはKeypressイベントを実行するたびにアプリケーションを起動するのに役立ちます。
tkinterでイベントをスケジュールするために、通常はbind ('Button'、callback)を使用します 方法。任意のキーをバインドして、アプリケーションで特定のタスクまたはイベントを実行できます。 Escをバインドするには アプリケーションウィンドウを閉じるようにキーを設定するには、キーとコールバックイベントを bind(key、callback)のパラメータとして渡す必要があります。 メソッド。
例
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Define the style for combobox widget
style = ttk.Style()
style.theme_use('xpnative')
# Define an event to close the window
def close_win(e):
win.destroy()
# Add a label widget
label = ttk.Label(win, text="Eat, Sleep, Code and Repeat", font=('Times New Roman italic', 18), background="black", foreground="white")
label.place(relx=.5, rely=.5, anchor=CENTER)
ttk.Label(win, text="Now Press the ESC Key to close this window", font=('Aerial 11')).pack(pady=10)
# Bind the ESC key with the callback function
win.bind('<Escape>', lambda e: close_win(e))
win.mainloop() 出力
上記のコードを実行すると、「Esc」キーを押すとすぐに閉じることができるウィンドウが表示されます。
次に、
-
Tkinterウィンドウを他のウィンドウの上に置く方法は?
GUIプログラムを作成するときはいつでも、tkinterは通常バックグラウンドで出力画面を表示します。言い換えれば、tkinterは他のプログラムの後ろにプログラムウィンドウを表示します。 tkinterウィンドウを他のウィンドウの上に配置するには、 attributes(-topmost、True)を使用する必要があります 財産。窓を上に引き上げます。 例 #Importing the library from tkinter import * #Create an instance of tkinter window or frame win= Tk() #Setting the ge
-
tkinterウィンドウを閉じるにはどうすればよいですか?
tkinterを使用してアプリケーションを作成するのは簡単ですが、タイトルバーのボタンを使用せずにウィンドウまたはフレームを閉じることが困難になる場合があります。このような場合、 .destroy()を使用できます ウィンドウを閉じる方法。 tkinter属性は互いに独立しているため、ボタンを使用してウィンドウを閉じる別のメソッドを作成できます。 例 #Import the library from tkinter import * #Create an instance of window win = Tk() #Set the geometry of the window win.g