[英]How to take tab as input from tkinter simpledialog.askstring()?
[英]How to take input from Tkinter
我正在使用Tkinter創建一個程序,用戶以磅為單位輸入重量,然后以千克為單位輸出重量。
我在從用戶那里獲取Entry
的內容時遇到問題。 我計算英鎊到基洛clicked1
。
有人可以告訴我如何獲得Entry輸入嗎?
from Tkinter import *
import tkMessageBox
class App(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("Question 7")
self.label = Label (self.root, text= "Enter your weight in pounds.")
self.label.pack()
self.entrytext = StringVar()
Entry(self.root, textvariable=self.entrytext).pack()
self.buttontext = StringVar()
self.buttontext.set("Calculate")
Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()
self.label = Label (self.root, text="")
self.label.pack()
self.root.mainloop()
def clicked1(self):
input = 3423 #I would like the user input here.
self.label.configure(text=input)
def button_click(self, e):
pass
App()
這是你要找的那種東西嗎?
from Tkinter import *
import tkMessageBox
class App(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("Question 7")
self.label = Label (self.root, text= "Enter your weight in pounds.")
self.label.pack()
self.entrytext = StringVar()
Entry(self.root, textvariable=self.entrytext).pack()
self.buttontext = StringVar()
self.buttontext.set("Calculate")
Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()
self.label = Label (self.root, text="")
self.label.pack()
self.root.mainloop()
def clicked1(self):
input = self.entrytext.get()
result = int(input)*2
self.label.configure(text=result)
def button_click(self, e):
pass
App()
我認為這是你正在尋找的,雖然不只是2的時間。如果值不是int,你可能還想要一個例外。
你要找的是[widget].get()
如果你使用文本小部件,你必須使用[widget].get(1.0, END)
其中1.0
表示“第一行,第0個字符”
我注意到代碼中的其他一些內容可以改進:
./script.py
執行它。 input
是一個內置函數,你應該避免覆蓋它 from Tkinter import *
。 這可能會導致意外的命名沖突。 ##!/usr/bin/env python
import Tkinter as Tk
class App(object):
def __init__(self):
self.root = Tk.Tk()
self.root.wm_title("Question 7")
self.label = Tk.Label(self.root, text="Enter your weight in pounds.")
self.label.pack()
self.weight_in_kg = Tk.StringVar()
Tk.Entry(self.root, textvariable=self.weight_in_kg).pack()
self.buttontext = Tk.StringVar()
self.buttontext.set("Calculate")
Tk.Button(self.root,
textvariable=self.buttontext,
command=self.clicked1).pack()
self.label = Tk.Label(self.root, text="")
self.label.pack()
self.root.mainloop()
def clicked1(self):
weight_in_kg = self.weight_in_kg.get()
self.label.configure(text=weight_in_kg)
def button_click(self, e):
pass
App()
由於您已將StringVar
與Entry
小部件相關聯,因此您可以使用StringVar的get和set方法輕松訪問/操作小部件的文本。
有關更多信息,請參見此處
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.