Python3 Tkinter How to bind events

Source: Internet
Author: User
I. Purpose

In the last article, "Python3 automatic generation of recorded records" said that by editing the bat file for parameter input is not very convenient, this is the use of Tkinter module to make input information interface, input complete click ' OK ' key, trigger the written script, make automatic generation of borrowed reconciliation record form. Second, tkinter installation instructions

python3.5 version, Tkinter is a built-in module and does not need to be installed separately. Use Tkinter, as long as the import tkinter. third, the idea of sorting out the original written to automatically generate bookkeeping record table script, the script encapsulated into a function a. Make an information input interface with Tkinter, enter merchant number, start time and end time, click OK key trigger parameter to pass into the encapsulated function A, and automatically generate borrowed reconciliation record table. Iv. Attach code Tkinter binding automatically generates an Accounting record form event with the following code:

Import Tkinter as TK
Import Time,datetime
From Tkinter Import *
Import Tkinter.messagebox
From ORACLE_SS import Get_oracle_data
/#运行的python文件和需要导入的python文件在一个目录的话, you do not need to write the package name ORUI.ORACLE_SS import. )

Class MainWindow:

def buttonListener1 (self, event): #创建事件, invoke the function of another function to generate a table Agent_no=self.text_agentno.get (1.0, Tk. End) #获取text_agentno文本框里的值 stime = Self.text_stime.get (1.0, Tk. End) ETime = Self.text_etime.get (1.0, Tk. End) #get_oracle_date () is a function of automatically generating the recorded records by itself, which can be called Get_oracle_data (agent_no,stime,etime) Tk.messagebox.sho
    Winfo ("MessageBox", "Cloud + Account Reconciliation record form has been automatically generated") def buttonListener2 (Self, event): #创建第二个事件, exit program exit () def __init__ (self): #创建Frame self.frame = Tk () # Sets the caption Self.frame.title (' Welcome, enter information: ') for the rectangular area of the frame #向frame中添加3个label self. Label_agentno = label (Self.frame, text= "merchant number:", fg= "Blue", "Verdana bold") Self.label_stime = label (SE Lf.frame, text= "Start time:", fg= "Blue", "Verdana bold") Self.label_etime = label (self.frame, text= "End Time:", fg= "BL"
    UE "," Verdana bold ") Self.text_agentno = text (Self.frame, height=" 1 ", width=30) #商户编号设置默认值为20000xxxxxxx Self.text_agentno.insert (1.0, ' 20000xxxxxxx ') self.teXt_stime = Text (Self.frame, height= "1", width=30) Now_time=datetime.datetime.now () yes_time=now_time+datetime.time Delta (days=-1) #开始时间设置默认值为前一天年月日0时0分0秒 Self.text_stime.insert (1.0,yes_time.strftime ("%y-%m-%d") + "00:00:00") s Elf.text_etime = text (Self.frame, height= "1", width=30) #结束时间设置默认值为当前时间年月日0时0分0秒 Self.text_etime.insert (1.0,now_ti Me.strftime ("%y-%m-%d") + "00:00:00") #使用grid来布局组件 specify where the component is placed Self.label_agentno.grid (row=0, column=0) SELF.L Abel_stime.grid (Row=1, column=0) Self.label_etime.grid (row=2, column=0) SELF.BUTTON_OK = button (Self.frame, text= "OK", width=10) Self.button_cancel = button (Self.frame, text= "Cancel", width=10) #使用grid设置各个label位置 Self.text _agentno.grid (row=0, Column=1) Self.text_stime.grid (row=1, Column=1) Self.text_etime.grid (row=2, column=1) se Lf.button_ok.grid (row=3, column=0) Self.button_cancel.grid (row=3, Column=1) self.button_ok.bind ("<Button-1>
", Self.buttonlistener1)    #绑定事件-click OK to tkinter automatically capture and trigger ButtonListener1 event, generate form Self.button_cancel.bind ("<Button-1>",
 SELF.BUTTONLISTENER2) #绑定事件-click the Cancel button, Tkinter automatically capture and trigger ButtonListener2 events, exit the program Self.frame.mainloop ()

The code for the Get_oracle_data () function of the

frame = MainWindow ()
Input ("Enter the" any press to exit) is no longer repeated here. As long as the previous "python3.5 implementation of automatic generation of accounting records," The code a little bit of encapsulation can be invoked. Five, summary for the Tkinter module, most of them are read Python official website documents and various related blogs are now learning to use, a lot of knowledge points may I also understand not thoroughly. For the interface needs to enter the start time and end time parameters, I would like to get a calendar plug-in, let the user choose the time, but looked at at least 2 more events: one is to click the button to trigger open calendar events, the other is to select the date trigger time refers to the delivery of events. Finally think or in the simplest way, with the insert () method to the start time and end time set a default value, the user can modify the default value of time, which is also convenient for users to use. Double-click on the written Aa.bat file, run the script automatically, the input information interface appears. Show interface, screenshot below:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.