py 9.9. Text Entries

9.9. Text Entries

The Entry widget allows text to be typedand displayed in a single line text box. The text may be set with methodcalls that allow new text to replace, prepend or append the current contentsof the Entry widget.

There are two functions for creating Entrywidgets:
 

entry = GtkEntry()

entry = GtkEntry(max)

The first just creates a new Entry widget,whilst the second creates a new Entry and sets a limit of maxcharacters on the length of the text within the Entry.

There are several methods for alteringthe text which is currently within the Entry widget.
 

entry.set_text(text)

entry.append_text(text)

entry.prepend_text(text)

The set_text()method sets the contents of the Entry widget to text,replacing the current contents. The append_text()and prepend_text() allowthe current contents to be appended or prepended with text.

The next method allows the current insertionpoint to be set.
 

entry.set_position(position)

The contents of the Entry can be retrievedby using a call to the following method. This is useful in the callbackmethods described below.
 

text = entry.get_text()

If we don't want the contents of the Entryto be changed by someone typing into it, we can change its editable state.
 

entry.set_editable(editable)

The above method allows us to toggle theeditable state of the Entry widget by passing in a TRUE or FALSE valuefor the editable argument.

If we are using the Entry where we don'twant the text entered to be visible, for example when a password is beingentered, we can use the following method, which also takes a boolean flag.
 

entry.set_visibility(visible)

A region of the text may be set as selectedby using the following method. This would most often be used after settingsome default text in an Entry, making it easy for the user to remove it.
 

entry.select_region(start, end)

If we want to be notified when the userhas entered text, we can connect to the activateor changed signal. Activateis raised when the user hits the enter key within the Entry widget. Changedis raised when the any change is made to the text, e.g. for every characterentered or removed.

The entry.pyexample program illustrates the use of an Entry widget. Figure 9.9 showsthe result of running the program:

Figure 9.9 Entry Example
The entry.py source code is:
 
    1   #!/usr/bin/env python
    2   
    3   # example entry.py
    4   
    5   import gtk
    6   
    7   class EntryExample:
    8       def enter_callback(self, widget, entry):
    9           entry_text = entry.get_text()
   10           print "Entry contents: %s\n" % entry_text
   11   
   12       def entry_toggle_editable(self, checkbutton, entry):
   13           entry.set_editable(checkbutton.active)
   14   
   15       def entry_toggle_visibility(self, checkbutton, entry):
   16           entry.set_visibility(checkbutton.active)
   17   
   18       def __init__(self):
   19           # create a new window
   20           window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   21           window.set_usize(200, 100)
   22           window.set_title("GTK Entry")
   23           window.connect("delete_event", gtk.mainquit)
   24   
   25           vbox = gtk.GtkVBox(gtk.FALSE, 0)
   26           window.add(vbox)
   27           vbox.show()
   28   
   29           entry = gtk.GtkEntry(50)
   30           entry.connect("activate", self.enter_callback, entry)
   31           entry.set_text("hello")
   32           entry.append_text(" world")
   33           entry.select_region(0, len(entry.get_text()))
   34           vbox.pack_start(entry, gtk.TRUE, gtk.TRUE, 0)
   35           entry.show()
   36   
   37           hbox = gtk.GtkHBox(gtk.FALSE, 0)
   38           vbox.add(hbox)
   39           hbox.show()
   40                                     
   41           check = gtk.GtkCheckButton("Editable")
   42           hbox.pack_start(check, gtk.TRUE, gtk.TRUE, 0)
   43           check.connect("toggled", self.entry_toggle_editable, entry)
   44           check.set_active(gtk.TRUE)
   45           check.show()
   46       
   47           check = gtk.GtkCheckButton("Visible")
   48           hbox.pack_start(check, gtk.TRUE, gtk.TRUE, 0)
   49           check.connect("toggled", self.entry_toggle_visibility, entry)
   50           check.set_active(gtk.TRUE)
   51           check.show()
   52                                      
   53           button = gtk.GtkButton("Close")
   54           button.connect_object("clicked", gtk.mainquit, window)
   55           vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
   56           button.set_flags(gtk.CAN_DEFAULT)
   57           button.grab_default()
   58           button.show()
   59           window.show()
   60   
   61   def main():
   62       gtk.mainloop()
   63       return 0
   64   
   65   if __name__ == "__main__":
   66       EntryExample()
   67       main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值