簡體   English   中英

如何獲取Python tkinter中的入口值?

[英]How to get the entry value in Python tkinter?

所以,我一直在編寫我的第一個代碼,一個權重轉換器來測試我學到的東西,並且我一直在嘗試獲取條目的值。 我嘗試使用 '''get''' function,但它不起作用。 難道我做錯了什么? 我應該嘗試以不同的方式格式化嗎?

這是我到目前為止所做的代碼(還沒有為函數編寫代碼)

import tkinter
from tkinter import *

root = tkinter.Tk()
root.title("Weight Conversion")

# Height and Width of the Main Window
height_ = 725
width_ = 625

exchange_values = {"pound": 2.20462, "ounce": 35.274, "dec_conv_value": 1000}

canvas = Canvas(root, height=height_, width=width_, bg="#80dfff")
canvas.pack()

frame = tkinter.Frame(root, bg="#ccffff")
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.75)

label = Label(frame, text="Kilograms", bg="#ccffff")
label.pack()

entry = Entry(frame)
entry.pack()


def convert():

    weight_grams = kg * exchange_values['dec_conv_value']
    weight_kilotons = kg / exchange_values['dec_conv_value']
    weight_pound = kg * exchange_values['pound']
    weight_ounce = kg * exchange_values['ounce']



button_ = tkinter.Button(frame, text="Convert", bg="#e6ffff", relief=GROOVE)
button_.pack()

root.mainloop()

你應該做entry_var.get()

這是一個簡單的例子:

import tkinter
from tkinter import *

root = tkinter.Tk()
root.title("Minimalistic Example")

label = Label(root, text="Function", bg="#ccffff")
label.pack()

def fnc():
    print(b1.get())


b1 = tkinter.Button(root, text="Convert", command=fnc)
b1.pack()

root.mainloop()

你必須定義一個變量然后把它放在入口小部件上

kilo = IntVar() # for integer value or
kilo = StringVar() #for string value
entry=Entry( frame,textvar= kilo) 
entry.pack()

def convert():
    kilo.get()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM