python gui - 002 tkinter button
2018-01-27 本文已影响0人
wsx9999
用python构建button组件的方法
from tkinter import * #载入tkinter模块
root = Tk()
def helloButton(): #构建button执行动作
print('Hello Button!')
Button(root, text = 'Hello Button!', command = helloButton).pack()
root.mainloop()

from tkinter import * #导入tkinter库
root = Tk()
def helloButton(): #构建button执行动作
print('Hello Button!')
#设置button样式
Button(root, text = 'hello button',command = helloButton,relief = FLAT).pack()
Button(root, text = 'hello button',command = helloButton,relief = GROOVE).pack()
Button(root, text = 'hello button',command = helloButton,relief = RAISED).pack()
Button(root, text = 'hello button',command = helloButton,relief = SOLID).pack()
Button(root, text = 'hello button',command = helloButton,relief = SUNKEN).pack()
root.mainloop()
输出如下

from tkinter import * #导入tkinter库
root = Tk()
Button(root, text='botton', compound='bottom', bitmap='error').pack()
Button(root, text='top', compound='top', bitmap='error').pack()
Button(root, text='right', compound='right', bitmap='error').pack()
Button(root, text='left', compound='left', bitmap='error').pack()
Button(root, text='center', compound='center', bitmap='error').pack()
root.mainloop()

使button加载位图
# -*- coding:UTF-8 -*-
from tkinter import * #载入tkinter模块
root = Tk()
img = PhotoImage(file = 'd:/1.gif') #如果和主程序不在同一文件夹下,则需要写入详细路径,如果和主程序在同一文件夹下,则只需要写文件名。如果在主程序所在的文件夹内的子文件夹内,则可以写’./****/**.gif‘
button = Button(root,image = img)
button.pack()
root.mainloop()
