TKinter

Tkinter

新手入门

import Tkinter as tk

app=tk.Tk()##创建Tk类
app.title("YUE~AN")##标题

thelabel=tk.Label(app,text="YUE AN")##标签,上面可以添加文本,图像等
thelabel.pack()##可以自动调节大小的方法

app.mainloop()
import Tkinter as tk
class APP:
    def __init__(self,name):
        ##做2个框架。
        frame=[]
        for i in range(2):
            frame.append(tk.Frame(name)) 
        frame[0].pack(side=tk.LEFT,padx=10,pady=3)  
        name.title("yue")        
        self.hi_there = tk.Button(frame[0],text="say hi~",bg="black",fg="white",command=self.say_hi)
        self.hi_there.pack() 
        ##还不能用~
        self.label=tk.Label(name,text="lalala!")
        self.label.pack()

    def say_hi(self):
        print("hello ~ !")


root = tk.Tk()
app = APP(root)
root.mainloop()

pack
我们使用 pack 函数的时候,默认先使用的放到上面,然 后 依次向下排,它会给我们的组件一个自认为合适的位置 和大小,这是默认方式。
每设置一次按钮,框架,标签后,即可通过pack方法来调整它们的位置。

引用块内容
pack布局非常简单,不用做过多的设置,直接使用一个 pack 函数就可以了。
可接受的参数:side:停靠在哪个方向
left: 左
top: 上
right: 右
botton: 下
fill:填充
x:水平方向填充
y:竖直方向填充
both:水平和竖直方向填充
none:不填充
expand:
yes:
no:
anchor:
N:北 下
E:东 右
S:南 下
W:西 左
CENTER:中间
padx:x方向的外边距
pady:y方向的外边距
ipadx:x方向的内边距
ipady:y方向的内边距
from Tkinter import *
root = Tk()
Button(root,text=’A’).pack(side=LEFT,expand=YES,fill=Y)
Button(root,text=’B’).pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text=’C’).pack(side=RIGHT,expand=YES,fill=NONE)
Button(root,text=’D’).pack(side=LEFT,expand=NO,fill=Y)
Button(root,text=’E’).pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text=’F’).pack(side=BOTTOM,expand=YES)
Button(root,text=’G’).pack(anchor=SE)
root.mainloop()

这里写图片描述

关于frame

from Tkinter import *  
root = Tk()  
#以不同的颜色区别各个frame  
for fm in ['red','blue','yellow','green','white','black']:  
    #注意这个创建Frame的方法与其它创建控件的方法不同,第一个参数不是root  
    Frame(height = 20,width = 400,bg = fm).pack()  
root.mainloop() 

这里写图片描述

2含有图片的标签

一张图片,一张文字

from Tkinter import *

def say_hi():
    print("hello ~ !")

root = Tk()

frame1 = Frame(root)
frame2 = Frame(root)
root.title("yue")

label= Label(frame1,text="lalalalalallalalalalllalal!",justify=LEFT)
label.pack(side=LEFT)

hi_there = Button(frame2,text="say hi~",command=say_hi)
hi_there.pack()

frame1.pack(padx=1,pady=1)
frame2.pack(padx=10,pady=10)

root.mainloop()

似乎当只存在一个标签或者button时候,不存在side?

from tkinter import *
# 导入tkinter模块的所有内容

root = Tk()

# 创建一个文本Label对象
textLabel = Label(root,
                  text="lalalal,\nlalalala!",
                  justify=LEFT,#文字左对齐
                  padx=10)
textLabel.pack(side=LEFT)

# 创建一个图像Label对象
# 用PhotoImage实例化一个图片对象(支持gif格式的图片)
photo = PhotoImage(file="18.gif")
imgLabel = Label(root, image=photo)
imgLabel.pack(side=RIGHT)

mainloop()

一张文字和背景图片

from tkinter import *

root = Tk()

photo = PhotoImage(file="bg.gif")
theLabel = Label(root,
                 text="lalla\nlalal",
                 justify=LEFT,
                 image=photo,
                 compound=CENTER,##表示文字覆盖在图像上
                 font=("宋体", 20),
                 fg="white"
                 )
theLabel.pack()

mainloop()

关于label

http://blog.csdn.net/red_sola/article/details/41950975

关于更改标签文字内容

from Tkinter import *

def callback():
    var.set("aaaaaaa")

root=Tk()

var = StringVar()#创建可以更改内容的对象
var.set("bbbbbb")
textLabel = Label(root,
                  textvariable=var,
                  justify=LEFT)
textLabel.pack(side=LEFT)

but=Button(root,text= u'变',command=callback)
but.pack()

mainloop()

上面的总结性代码

from Tkinter import *
def callback():
    var1.set("Noo~ these change !")
root = Tk()

frame1 = Frame(root)
frame2 = Frame(root)
#可变字符串
var1 = StringVar()
var1.set("If you push the button\nthese contents could change !")
#背景图片 只支持gif
image = PhotoImage(file='bg.gif')

label = Label(frame1,
              textvariable=var1,
              justify=LEFT,
              image = image,
              compound=CENTER,
              font=20,
              fg="white"
              )
label.pack()
frame1.pack()

button = Button(frame2,
                text="broken English !",
                command = callback)
button.pack(side=LEFT,padx=10,pady=10)

image2 = PhotoImage(file="18.gif")
label2 = Label(frame2,
               image= image2,
               compound=CENTER,
               )
label2.pack()
frame2.pack()
mainloop()
Abstract Describes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Python programming language. This publication is available in Web form1 and also as a PDF document2. Please forward any comments to tcc-doc@nmt.edu. Table of Contents 1. What is Tkinter?.......................................................................................................................3 2. A minimal application..............................................................................................................3 3. Definitions..............................................................................................................................4 4. Layout management.................................................................................................................5 4.1. The .grid() method....................................................................................................5 4.2. Other grid management methods...................................................................................6 4.3. Configuring column and row sizes.................................................................................7 4.4. Making the root window resizeable................................................................................8 5. Standard attributes...................................................................................................................8 5.1. Dimensions...................................................................................................................9 5.2. The coordinate system...................................................................................................9 5.3. Colors...........................................................................................................................9 5.4. Type fonts...................................................................................................................10 5.5. Anchors......................................................................................................................11 5.6. Relief styles.................................................................................................................12 5.7. Bitmaps.......................................................................................................................12 5.8. Cursors.......................................................................................................................12 5.9. Images........................................................................................................................14 5.10. Geometry strings........................................................................................................14 5.11. Window names...........................................................................................................15 5.12. Cap and join styles.....................................................................................................15 5.13. Dash patterns.............................................................................................................16 5.14. Matching stipple patterns............................................................................................16 6. The Button widget................................................................................................................17 7. The Canvas widget................................................................................................................19 7.1. Canvas coordinates......................................................................................................20 7.2. The Canvas display list................................................................................................20 7.3. Canvas object IDs........................................................................................................21 7.4. Canvas tags................................................................................................................21 1http://www.nmt.edu/tcc/help/pubs/tkinter/ 2http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf 1 Tkinter reference New Mexico Tech Computer Center 7.5. CanvastagOrId arguments......................................................................................21 7.6. Methods on Canvas widgets........................................................................................21 7.7. Canvas arc objects.......................................................................................................26 7.8. Canvas bitmap objects.................................................................................................28 7.9. Canvas image objects..................................................................................................29 7.10. Canvas line objects.....................................................................................................29 7.11. Canvas oval objects....................................................................................................31 7.12. Canvas polygon objects..............................................................................................32 7.13. Canvas rectangle objects.............................................................................................34 7.14. Canvas text objects.....................................................................................................35 7.15. Canvas window objects..............................................................................................36 8. The Checkbutton widget......................................................................................................37 9. The Entry widget..................................................................................................................40 9.1. Scrolling an Entry widget............................................................................................43 10. The Frame widget................................................................................................................43 11. The Label widget................................................................................................................44 12. The LabelFrame widget......................................................................................................46 13. The Listbox widget............................................................................................................48 13.1. Scrolling a Listbox widget........................................................................................52 14. The Menu widget..................................................................................................................52 14.1. Menu item creation (coption) options.........................................................................55 14.2. Top-level menus.........................................................................................................56 15. The Menubutton widget......................................................................................................57 16. The Message widget............................................................................................................59 17. The OptionMenu widget.......................................................................................................60 18. The PanedWindow widget....................................................................................................61 18.1. PanedWindow child configuration options...................................................................63 19. The Radiobutton widget....................................................................................................64 20. The Scale widget................................................................................................................67 21. The Scrollbar widget........................................................................................................70 21.1. The Scrollbarcommand callback............................................................................72 21.2. Connecting a Scrollbar to another widget................................................................73 22. The Spinbox widget............................................................................................................73 23. The Text widget..................................................................................................................78 23.1. Text widget indices...................................................................................................80 23.2. Text widget marks....................................................................................................81 23.3. Text widget images...................................................................................................82 23.4. Text widget windows...............................................................................................82 23.5. Text widget tags.......................................................................................................82 23.6. Setting tabs in a Text widget......................................................................................83 23.7. The Text widget undo/redo stack..............................................................................83 23.8. Methods on Text widgets..........................................................................................84 24. Toplevel: Top-level window methods..................................................................................91 25. Universal widget methods.....................................................................................................93 26. Standardizing appearance...................................................................................................101 26.1. How to name a widget class......................................................................................102 26.2. How to name a widget instance.................................................................................102 26.3. Resource specification lines.......................................................................................102 26.4. Rules for resource matching......................................................................................103 27. Connecting your application logic to the widgets...................................................................104 28. Control variables: the values behind the widgets...................................................................104 29. Focus: routing keyboard input.............................................................................................106 New Mexico Tech Computer Center Tkinter reference 2 30. Events................................................................................................................................107 30.1. Levels of binding......................................................................................................108 30.2. Event sequences.......................................................................................................109 30.3. Event types..............................................................................................................109 30.4. Event modifiers........................................................................................................110 30.5. Key names...............................................................................................................111 30.6. Writing your handler: The Event class......................................................................113 30.7. The extra arguments trick..........................................................................................115 30.8. Virtual events...........................................................................................................116 31. Pop-up dialogs....................................................................................................................116 31.1. The tkMessageBox dialogs module..........................................................................116 31.2. The tkFileDialog module.....................................................................................118 31.3. The tkColorChooser module.................................................................................119
python开发的真实星空显示软件 含真实恒星位置数据3144颗 代码讲解见: https://blog.csdn.net/xiaorang/article/details/106598307 数据格式例: {'long': 0.023278328898474372, 'lat': -0.09961466705757636, 'light': 46, 'const': 66}, {'long': 0.024870941840919196, 'lat': 0.2338062439126301, 'light': 55, 'const': 62}, {'long': 0.028107061526797, 'lat': 1.1204335039257496, 'light': 56, 'const': 18}, {'long': 0.03660100303760025, 'lat': 0.5077259659824991, 'light': 21, 'const': 1}, {'long': 0.04004802831028905, 'lat': 1.0323574005393255, 'light': 23, 'const': 18}, {'long': 0.03944444109507185, 'lat': 0.3178583859888262, 'light': 55, 'const': 62}, {'long': 0.040797071265367454, 'lat': -0.488478858963941, 'light': 54, 'const': 74}, {'long': 0.0410661312228549, 'lat': -0.798444499556106, 'light': 39, 'const': 64}, {'long': 0.043800486202076855, 'lat': 0.1945266317121166, 'light': 55, 'const': 66}, {'long': 0.045036755271142, 'lat': 0.804111967609767, 'light': 50, 'const': 1}, {'long': 0.043785947609407745, 'lat': -1.4350775693910554, 'light': 53, 'const': 58}, {'long': 0.04915283505929031, 'lat': -0.2699684886295715, 'light': 49, 'const': 21}, {'long': 0.050498187206605094, 'lat': -0.4851966800391031, 'light': 54, 'const': 74}, {'long': 0.05119631890740283, 'lat': -0.6131874860342564, 'light': 52, 'const': 74}, {'long': 0.05775584219505068, 'lat': 0.26500400429202875, 'light': 28, 'const': 62}, {'long': 0.05896303407877759, 'lat': 0.7162006931179011, 'light': 57, 'const': 1}, {'long': 0.06371905629046214, 'lat': 0.3526728525507925, 'light': 48, 'const': 62}, {'long': 0.06387905062299246, 'lat': -0.33043929519585447, 'light': 44, 'const': 21}, 代码解说详细的教程见: https://blog.csdn.net/xiaorang/article/details/106598307
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值