Python GUI's Tkinter grammar essays

Source: Internet
Author: User

Readily written, followed by finishing.

1. Entry how to invoke the __init__ of the parent class?

Following errors, reasons for subsequent analysis

Super (Your_entry_class, self). __init__ (SELF,**KW)

Error: Typeerror:must is type, not instance. Somehow

Google out, the solution is as follows

Apply (entry.__init__, (self, parent), **KW)

 

2. How to transfer a row from the TTK TreeView to an edit state

In fact, (Python 3) The TreeView has a ReadOnly property. But I'm using Python 2.

Solution: Position the cell according to the mouse position when you double-click the TreeView, and then paste a entry control into the cell. Referenced from StackOverflow.

    defOn_detail_bom_line_db_click (Self, event):" "Executed, when a row is double-clicked. Opens read-only Entrypopup above the item ' s column, so it's possible to select text" "        #Close Previous popups        ifSelf.entryPopup:self.entryPopup.destroy ()#What row and column is clicked onrowID=Self.bom_lines.identify_row (EVENT.Y) column=Self.bom_lines.identify_column (event.x)#clicked Row Parent IDParent =self.bom_lines.parent (ROWID)#print ' Parent: ' +parent        #Do nothing if item is top-level        ifParent = ="':            Pass        #Get column Position infoX,y,width,height =Self.bom_lines.bbox (rowid, column)#Y-axis offsetPady = height//2#Place Entry Popup properlyurl = Self.bom_lines.item (rowID,'text') Self.entrypopup= Stickyentry (self.bom_lines, URL, width=12) self.entryPopup.place (x=x, Y=y+pady, Anchor=w)

Extended the entry, adding a little bit of behavior:

Class Stickyentry (Entry):    def __init__ (self, parent, text, **kw):        "If relwidth is set and then width is ignored" '        #fa = Super (Self,stickyentry)        #fa. __init__ (parent, **kw)        apply (entry.__init__, (self, parent), kw)        Self.insert (0, text)        #self [' state '] = ' readonly '        self[' readonlybackground '] = ' white '        self[' Selectbackground '] = ' #1BA1E2 '        self[' exportselection '] = False        self.focus_force ()        Self.bind ("< Control-a> ", Self.selectall)        self.bind (" <Escape> ", Lambda *ignore:self.destroy ())    def SelectAll (Self, *ignore):        "Set selection on the whole text" '        self.selection_range (0, ' end ')        # returns ' Break ' t o Interrupt default key-bindings        return ' break '

3. What is the Heigth property of the TreeView?

For example, set to (pixels), the result throws a lot of screen. The reason is this is the number of lines!!!

4. The width of the Entry does not seem to be in pixels!

Simply set a number, such as 20, to have 200pixel or more effects.

After verification, it seems that the number of numeric characters (such as 20, just can put 20 numeric characters!) )

The exact character of the reference is unknown!

5. How do I set the vertical scrollbar for the TTK TreeView?

Add a Srollbar control and throw its set function to the Yscrollcommand of the TreeView to bind.

Note the layout, the ScrollBar control is best next to the TreeView, and on the right.

Vbar = TTK. ScrollBar (container, orient=vertical, command = Your_treeview.yview) your_treeview.configure (yscrollcomand=vbar.set ) Your_treeview.grid (row=0) Vbar.grid (row=0, Column=1,sticky=ns)

6. How do I empty the contents of the TTK treeview?

Call its Delete method, as follows:

Items = Your_treeview.get_children () [Your_treeview.delete (item) for item in items]

7. How do I get the value of the cell TTK the TreeView is double-clicked?

Theoretically, only through the control is no solution. The resolution is defined by the way you define the boundaries of the coordinates, which defines the width (in pixel units) for each column. Event.x/y can take the coordinates. You can analyze the coordinate range of the column in which the coordinate falls.

Header_widths=[n1,n2,n3 ....] row_selected = Your_treeview.identify_row (event.y) values = Your_treeview.item (row_selected, ' values ') Cell_val = Falseindex = -1t=event.xfor i in range (header_widths):    if t>0:        t=t-self.form_bom_line_widths[i]    else:        index=i-1        breakif index!=-1:    cell_val = Values[index]

  

Python GUI's Tkinter grammar essays

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.