zoukankan      html  css  js  c++  java
  • tkinter简单使用

    第一个运行程序

    # -*- coding: utf-8 -*-
    import tkinter as tk   //引入
    root = tk.Tk()     // 实例化root    T大写k小写
    root.title('Demo')
    theLabel = tk.Label(root, text='我的第二个窗口程序!')
    theLabel.pack()
    root.mainloop()

    ###运行###

    ###部分参数###

    pack(side =tk.LEFT, padx =0, pady=0) 靠左
    pack(side =tk.TOP, padx =0, pady=0) 靠上
    pack(side =tk.RIGHT, padx =0, pady=0) 靠右
    pack(side =tk.TOTTOM, padx =0, pady=0) 靠下
    text = '文字'
    bg = '' 背景色
    fg = '' 前景色、
    #窗口居中显示
    root.geometry('%dx%d+%d+%d' % (width,height,(root.winfo_screenwidth() - width ) / 2, (root.winfo_screenheight() - height) / 2))
    #窗口最大值
    root.maxsize(600,600)
    #窗口最小值
    root.minsize(600,600)

    结合面向对象

     

    import tkinter as tk
    class App():
        def __init__(self,root):
            frame = tk.Frame(root)
            frame.pack(side=tk.LEFT, padx=2)
            self.hi = tk.Button(frame, text='点击我', fg ='white', bg='red',command = self.hello)
            self.hi.pack()
        def hello(self):
            print 'hello!'
    root = tk.Tk()
    root.title("TkinterSimple")
    width ,height= 600, 600
    #窗口居中显示
    root.geometry('%dx%d+%d+%d' % (width,height,(root.winfo_screenwidth() - width ) / 2, (root.winfo_screenheight() - height) / 2))
    #窗口最大值
    root.maxsize(600,600)
    #窗口最小值
    root.minsize(600,600)
    app = App(root)
    root.mainloop()

    ###运行###

    设计的确不美观,对齐的话的确不好操作,但是可以通过别的方法。

  • 相关阅读:
    [CF451E] Devu and Flowers
    [CF1038E] Maximum Matching
    [CF825E] Minimal Labels
    [CCPC2020绵阳L] Lottery
    [CCPC2020绵阳K] Knowledge is Power
    [CCPC2020绵阳J] Joy of Handcraft
    [CCPC2020绵阳G] Game of Cards
    [CCPC2020绵阳D] Defuse the Bombs
    [CF1082E] Increasing Frequency
    [CF301B] Yaroslav and Time
  • 原文地址:https://www.cnblogs.com/shuangzikun/p/python_taotao_tkinter_first.html
Copyright © 2011-2022 走看看