我正在尝试使用按钮进行一些输入,但我必须使用Key来首先进行一个interraction to entry widget。我想在没有它的情况下实现它,但问题是当你进行多次输入时有没有办法用mause click和cursor选择那个条目? 这是我的代码。
# -*- coding: utf-8 -*-
from Tkinter import*
import sys
pencere = Tk()
#pencere.overrideredirect(1)#başlıksız pencere
boyut = pencere.geometry ("400x400+250+150")#x*y +x+y boyut ve konum
baslik = pencere.title("guc hesaplama programi") #pencere başlığı
say=0
def click(key):
global say
if say==0:
if key == 'C':
joule_entry.delete(0, END) # clear entry
else:
joule_entry.insert(END, key)
if say==1:
if key == 'C':
time_entry.delete(0, END) # clear entry
else:
time_entry.insert(END, key)
if say==2:
if key == 'C':
power_entry.delete(0, END) # clear entry
else:
power_entry.insert(END, key)
def write_joule():
global say
say=0
def write_time():
global say
say=1
def write_power():
global say
say=2
def joule_find():
joule_entry.delete(0,END)
power_t = power_entry.get()
power = float(power_t)
time_t = time_entry.get()
time = float(time_t)
joule = time*power/1000
if 0 <= power <= 1500 and 0 <= time <= 9999: #sınırlamalarımız
joule_entry.insert(0,str(round(joule,3)))
else:
joule_entry.insert(0,"error")
def time_find():
time_entry.delete(0,END)
power_t = power_entry.get()
power = float(power_t)
joule_t = joule_entry.get()
joule = float(joule_t)
time = joule/(power/1000)
if 0 <= power <= 1500 and 0 <= time <= 9999: #sınırlamalarımız
time_entry.insert(0,str(round(time,3)))
else:
time_entry.insert(0,"error")
def power_find():
power_entry.delete(0,END)
joule_t = joule_entry.get()
joule = float(joule_t)
time_t = time_entry.get()
time = float(time_t)
guc = 1000*joule/time
if 0 <= guc <= 1500 and 0 <= time <= 9999: #sınırlamalarımız
power_entry.insert(0,str(round(guc,3)))
else:
power_entry.insert(0,"error")
joule_button = Button(text="ENERGY\t:" ,font="arial 14 bold", command = joule_find) #
joule_button.grid(row=0, column=0, sticky="w", pady=10)
joule_entry = Entry()
joule_entry.grid(row=0, column=1, pady=10,sticky="news")
joule_button2 = Button(text="key" ,font="arial 14 bold", command = write_joule) #
joule_button2.grid(row=0, column=3, sticky="w", pady=10)
joule_label = Label(text="Joule" ,font="arial 14 bold")
joule_label.grid(row=0, column=2, sticky="w", pady=10)
time_button = Button(text="TIME\t:",font="arial 14 bold", command = time_find )
time_button.grid(row=1, column=0, sticky="w", pady=10)
time_entry= Entry()
time_entry.grid(row=1, column=1, pady=10,sticky="news")
time_button2 = Button(text="key",font="arial 14 bold", command = write_time )
time_button2.grid(row=1, column=3, sticky="w", pady=10)
time_label= Label(text="Second",font="arial 14 bold" )
time_label.grid(row=1, column=2, sticky="w")
power_button = Button(text="Power\t:",font="arial 14 bold", command = power_find )
power_button.grid(row=2, column=0, sticky="w", pady=10)
power_entry= Entry()
power_entry.grid(row=2, column=1, pady=10,sticky="news")
power_button2 = Button(text="key",font="arial 14 bold", command = write_power )
power_button2.grid(row=2, column=3, sticky="w", pady=10)
power_label= Label(text="milli Watt",font="arial 14 bold" )
power_label.grid(row=2, column=2, sticky="w")
close = Button(text = "CLOSE",command = pencere.destroy)
close.grid(row=40,column=2,sticky="news")
btn_list = [
'7', '8', '9',
'4', '5', '6',
'1', '2', '3',
'0', '.', 'C',]
# create all buttons with a loop
r = 5
c = 0
for b in btn_list:
rel = 'ridge'
cmd = lambda x=b: click(x)
Button(text=b,width=5,relief=rel,command=cmd).grid(row=r,column=c,sticky="news")
c += 1
if c > 2:
c = 0
r += 1
mainloop()