tkinter, bagaimana cara mendapatkan nilai widget Entri? [duplikat]

Saya mencoba menawarkan kepada pengguna kemungkinan untuk menghitung keuntungan dari proyeksi penjualannya jika margin memiliki nilai tertentu (0,23). Pengguna harus dapat memasukkan nilai apa pun sebagai proyeksi penjualan:

from tkinter import *

root = Tk()

margin = 0.23
projectedSales = #value of entry
profit = margin * int(projectedSales)

#My function that is linked to the event of my button
def profit_calculator(event):
    print(profit)


#the structure of the window
label_pan = Label(root, text="Projected annual sales:")
label_profit = Label(root, text="Projected profit")
label_result = Label(root, text=(profit), fg="red")

entry = Entry(root)

button_calc = Button(root, text= "Calculate", command=profit_calculator)
button_calc.bind("<Button-1>", profit_calculator)

#position of the elements on the window
label_pan.grid(row=0)
entry.grid(row=0, column=1)
button_calc.grid(row=1)              
label_profit.grid(row=2)
label_result.grid(row=2, column=1)

root.mainloop()

person Pak    schedule 19.11.2017    source sumber
comment
Pertanyaan awal apa mencakup cara menggunakan teks Entri sebagai variabel juga.   -  person Nae    schedule 20.11.2017


Jawaban (1)


Anda bisa mendapatkan apa yang ada di dalam widget Entri menggunakan metode get seperti:

entry = tkinter.Entry(root)
entryString = entry.get()

Berikut ini contoh yang sesuai dengan apa yang Anda inginkan:

import tkinter as tk

root = tk.Tk()

margin = 0.23

entry = tk.Entry(root)

entry.pack()

def profit_calculator():
    profit = margin * int(entry.get())
    print(profit)

button_calc = tk.Button(root, text="Calculate", command=profit_calculator)
button_calc.pack()

root.mainloop()

Anda mungkin juga ingin menggunakan opsi textvariable dan kelas tkinter.IntVar() untuk menyinkronkan teks bilangan bulat untuk beberapa widget seperti:

import tkinter as tk

root = tk.Tk()

margin = 0.23
projectedSales = tk.IntVar()
profit = tk.IntVar()

entry = tk.Entry(root, textvariable=projectedSales)

entry.pack()

def profit_calculator():
    profit.set(margin * projectedSales.get())

labelProSales = tk.Label(root, textvariable=projectedSales)
labelProSales.pack()

labelProfit = tk.Label(root, textvariable=profit)
labelProfit.pack()

button_calc = tk.Button(root, text="Calculate", command=profit_calculator)
button_calc.pack()

root.mainloop()

Contoh di atas menunjukkan bahwa labelProSales dan entry memiliki nilai text yang sama sepanjang waktu, karena keduanya menggunakan variabel yang sama, projectedSales, sebagai opsi textvariable.

person Nae    schedule 19.11.2017
comment
Oke, terima kasih banyak atas jawaban Anda, saya mengharapkan jawaban seperti ini. Sepertinya aku mulai mengerti! - person Pak; 20.11.2017
comment
Maaf, tapi di sini tertulis kesalahan: - person ; 29.08.2020
comment
@TroyD Kesalahan itu tidak terkait dengan potongan kode di atas karena variabel inputS tidak disebutkan sama sekali. Untuk bantuan lebih lanjut mengenai kesalahan tersebut, saya sarankan untuk membaca di ericlippert.com/2014/03/05/how-to-debug-small-programs - person Nae; 31.08.2020