カスタムポップアップTkinterダイアログボックスを実装する正しい方法は何ですか?
Tkinterには、Pythonですでに実装されている多くの組み込み関数とモジュールがあります。 メッセージボックス Tkinterのモジュールは、関連する機能を使用するだけで、あらゆるアプリケーションで使用できるモジュールの1つです。これらのパッケージの唯一の制限は、メッセージボックスを変更または変更できないことです。 レンプレート。したがって、カスタムポップアップメッセージボックスを実装するには、次の手順に従います。
- ボタンを作成し、それに関数を定義するコマンドを追加します。
- トップレベルウィンドウを作成し、それに他のウィジェットを追加する関数を定義します。
- トップレベルウィンドウにボタンと確認ラベルテキストを追加します。
- ボタンコマンドを追加して、メインウィンドウにメッセージをインタラクティブに表示します。
例
# Import required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the window size
win.geometry("700x250")
# Define a function to implement choice function
def choice(option):
pop.destroy()
if option == "yes":
label.config(text="Hello, How are You?")
else:
label.config(text="You have selected No")
def click_fun():
global pop
pop = Toplevel(win)
pop.title("Confirmation")
pop.geometry("700x250")
pop.config(bg="green3")
# Create a Label Text
label = Label(pop, text="Would You like to Proceed?", bg="green3", fg="white", font=('Aerial', 12))
label.pack(pady=20)
# Add a Frame
frame = Frame(pop, bg="green3")
frame.pack(pady=10)
# Add Button for making selection
button1 = Button(frame, text="Yes",
command=lambda: choice("yes"), bg="green")
button1.grid(row=0, column=1)
button2 = Button(frame, text="No",
command=lambda: choice("no"), bg="green")
button2.grid(row=0, column=2)
# Create a Label widget
label = Label(win, text="", font=('Aerial', 14))
label.pack(pady=40)
# Create a Tkinter button
ttk.Button(win, text="Click Here", command=click_fun).pack()]
win.mainloop() 出力
上記のコードを実行すると、ボタン付きのウィンドウが表示されます。
ボタンをクリックすると、カスタムポップアップメッセージボックスが表示されます
-
Tkinterラベルのテキストを取得するにはどうすればよいですか?
Tkinterラベルは、ウィンドウにテキストまたは画像を作成して表示するために使用されます。フォントファミリー、パディング、幅、高さなどのラベル情報をカスタマイズするために使用できるいくつかのコンポーネントと関数があります。ウィンドウにラベルテキストを表示するために、必要なテキストの値を書き込むことができます。ウィンドウに表示されます。 例 #Import the required library from tkinter import * #Create an instance of tkinter frame win= Tk() #Define the geometry of the
-
Tkinterでのマウスカーソルの変更
TkinterはGUIベースのPythonライブラリであり、さまざまなタイプの機能的およびGUIベースのアプリケーションを開発するために使用されます。アプリケーションの開発中に拡張性とさまざまな機能を提供するために使用できる多くの機能とメソッドを提供します。 この記事では、cursorプロパティを使用してtkinterフレームのボタンにカーソルを合わせながらマウスカーソルを変更する方法を説明します。 tkinterのボタンライブラリには、エンドユーザーにさまざまなビジュアルを提供するカーソルマップがたくさんあります。ライブラリ内のカーソルの一部は、 「矢印」 「サークル」 「時計」