1. <dd id="erndk"></dd>
                1. python 窗口

                  互聯網 2022/5/2 11:12:38

                  一、界面 1、創建窗口 import tkinter#主窗口 win = tkinter.Tk()#窗口標題 win.title("hello word")#設置位置,寬,高,距離左方,距離上方 win.geometry("400x100+200+100")#顯示窗口 win.mainloop()2、標簽控件label #顯示文本控件label label = tkin…

                  一、界面

                  1、創建窗口

                  import tkinter
                  
                  #主窗口
                  win = tkinter.Tk()
                  
                  #窗口標題
                  win.title("hello word")
                  
                  #設置位置,寬,高,距離左方,距離上方
                  win.geometry("400x100+200+100")
                  
                  #顯示窗口
                  win.mainloop()
                  

                  2、標簽控件label

                  #顯示文本控件label
                  label = tkinter.Label(win,
                                        text = "good morning",#文本內容
                                        bg = "pink",#文本背景顏色
                                        fg = "red",#字體顏色
                                        font = ("宋體",20),#什么字體,字體大小
                                        height = 10,#文本內容的高
                                        width = 20,#文本內容的寬
                                        wraplength = 100,#文本中多寬進行換行
                                        justify = "left",#換行之后的對齊方式
                                        anchor = "center"#文本位置,n、e、s、w、ne、es、sw、wn
                                        )
                  
                  #將控件顯示出來
                  label.pack()
                  

                  3、按鈕

                  def func():
                      print("hello")
                  
                  button = tkinter.Button(win,
                                          text="按鈕",  #按鈕上的字
                                          command=func,#點擊按鈕執行函數,可以執行匿名函數
                                          width = 10,#寬
                                          height = 20#高
                                          )
                  button.pack()
                  

                  4、單行文本框

                  e = tkinter.Variable()
                  entry = tkinter.Entry(win,#主窗口
                                        #show="*",#文本框中的內容以*替代,相當于密碼框
                                        textvariable=e#綁定變量,e就是entry對象
                                        )
                  entry.pack()
                  #文本框設置值
                  e.set("hello word")
                  #取出文本框內的內容
                  print(e.get())
                  print(entry.get())
                  

                  5、多行文本框

                  #滾動條
                  scroll =tkinter.Scrollbar()
                  #多行文本框,width幾列,height幾行
                  text = tkinter.Text(win,
                                      width=50,
                                      height = 8
                                      )
                  #side放在窗口的哪一側,fill填充
                  scroll.pack(side = tkinter.RIGHT, fill=tkinter.Y)
                  text.pack(side = tkinter.LEFT, fill = tkinter.Y)
                  
                  #關聯
                  scroll.config(command=text.yview)
                  text.config(yscrollcommand=scroll.set)
                  
                  str = '''
                  The cynics say the college students are the pampered lost generation, which would cringe at the slightest discomfort. But the cynics are wrong. The college students I see are eagerly learning about how to live independently. We help each other clean the dormitory, go shopping and bargain together, and take part time jobs to supplement our pocket money.
                  Recently, ther is a heated debate in our society. The college students are the beneficiaries of a rare privilege, who receive exceptional education at extraordinary places. But will we be able to face the challenge and support ourselves against all odds? Will we be able to better the lives of others? Will we be able to accept the responsibility of building the future of our country?
                  '''
                  
                  text.insert(tkinter.INSERT,str)
                  

                  6、復選框

                  def updata():
                      message = ""
                      if hobby1.get() == True:
                          message += "tom\n"
                      if hobby2.get() == True:
                          message += "jim\n"
                      if hobby3.get() == True:
                          message += "lily\n"
                  
                      #刪除文本框內容
                      text.delete(0.0,tkinter.END)
                      #將message添加到文本框中
                      text.insert(tkinter.INSERT,message)
                  
                  
                  hobby1 = tkinter.BooleanVar()
                  hobby2 = tkinter.BooleanVar()
                  hobby3 = tkinter.BooleanVar()
                  
                  #創建復選框
                  check1 = tkinter.Checkbutton(win,#主窗口
                                               text="tom",#多選框標題
                                               variable=hobby1,#綁定變量
                                               command=updata#函數
                                               )
                  #顯示復選框
                  check1.pack()
                  check2 = tkinter.Checkbutton(win,
                                               text="jim",
                                               variable=hobby2,
                                               command=updata
                                               )
                  #顯示復選框
                  check2.pack()
                  check3 = tkinter.Checkbutton(win,
                                               text="lily",
                                               variable=hobby3,
                                               command=updata
                                               )
                  #顯示復選框
                  check3.pack()
                  text = tkinter.Text(win,width=50,height=5)
                  text.pack()
                  

                  7、單選框

                  def updata():
                      print(r.get())
                  
                  r =tkinter.IntVar()
                  radio1 = tkinter.Radiobutton(win,
                                               text="one",#按鈕名字
                                               value = 44,#按鈕的值,函數打印的就是值,函數打印的就是
                                               variable=r,#綁定變量
                                               command=updata
                                               )
                  radio1.pack()
                  radio2 = tkinter.Radiobutton(win,
                                               text="one",
                                               value = 45,
                                               variable=r,
                                               command=updata
                                               )
                  radio2.pack()
                  

                  8、列表框

                  #列表框
                  lb = tkinter.Listbox(win,
                                       selectmode=tkinter.BROWSE
                                       )
                  lb.pack()
                  #往列表框中添加數據
                  for i in ['good','nice','very good']:
                      #在結尾添加
                      lb.insert(tkinter.END,i)
                  #往列表框中第一行添加數據
                  lb.insert(tkinter.ACTIVE, 'COOL')
                  #刪除下標索引為2-3行的數據,也可以只填2
                  #lb.delete(2,3)
                  
                  #選中下標索引為2-3行的數據,也可以只填2,取消選中為lb.select_clear,用法和選中一樣
                  lb.select_set(2,3)
                  #獲取列表元素個數
                  #print(lb.size())
                  
                  #獲取列表中的值,第2-3行的值,也可以只填2
                  #print(lb.get(2,3))
                  
                  #返回當前的下標索引
                  print(lb.curselection())
                  #判斷一個選項是否被選中
                  print(lb.select_includes(2))
                  

                  9、列表框綁定事件

                  #綁定變量
                  lbv = tkinter.StringVar()
                  #不支持鼠標移動選中選中位置,支持鼠標按下選中位置
                  lb = tkinter.Listbox(win,
                                       selectmode = tkinter.SINGLE,
                                       listvariable = lbv
                                       )
                  lb.pack()
                  for i in ['good','nice','very good']:
                      #在結尾添加
                      lb.insert(tkinter.END,i)
                  #print(lbv.get())
                  #設置選項
                  #lbv.set(('1','2','3'))
                  
                  #綁定事件
                  def MyPrint(event):
                      #打印雙擊選中的值
                      print(lb.get(lb.curselection()))
                  #雙擊選中執行函數MyPrint
                  lb.bind('<Double-Button-1>',MyPrint)
                  

                  10、列表框多選(全選)

                  lb = tkinter.Listbox(win,
                                       selectmode = tkinter.EXTENDED
                                       )
                  lb.pack()
                  for i in ['good','nice','very good','zzz','ffff','good','nice','very good','zzz','ffff''good','nice','very good','zzz','ffff''good','nice','very good','zzz','ffff']:
                      lb.insert(tkinter.END,i)
                  

                  11、菜單欄

                  menubar = tkinter.Menu(win)
                  win.config(menu = menubar)
                  
                  #創建一個菜單選項
                  menul = tkinter.Menu(menubar,tearoff = False)
                  #給菜單選項添加內容
                  for i in ['C','PHP','C++','JAVA','JS','quit']:
                      if i == 'quit':
                          menul.add_command(label=i,command=win.quit)
                      else:
                          menul.add_command(label=i)
                  
                  #向菜單條上添加菜單選項
                  menubar.add_cascade(label="語言",menu=menul)
                  

                  12、鼠標右鍵菜單欄

                  menubar = tkinter.Menu(win)
                  
                  menu = tkinter.Menu(menubar,tearoff = False)
                  for i in ['C','PHP','C++','JAVA','JS','quit']:
                      menu.add_command(label=i)
                  
                  menubar.add_cascade(label="語言", menu=menu)
                  #顯示菜單
                  def showMenu(event):
                      menubar.post(event.x_root, event.y_root)
                  #鼠標右鍵調用函數showMenu顯示菜單
                  win.bind('<Button-3>',showMenu)
                  
                  

                  13、下拉菜單

                  from  tkinter import ttk
                  #綁定變量
                  cv = tkinter.StringVar()
                  com = ttk.Combobox(win,textvariable=cv)
                  com.pack()
                  #下拉菜單的值
                  com['value'] = ('C','C++','JS')
                  #設置默認值
                  com.current(0)
                  
                  def func(event):
                      print(cv.get())
                  #綁定事件
                  com.bind('<<ComboboxSelected>>',func)
                  

                  14、窗口布局

                  # 在主窗口win上布局
                  frm = tkinter.Frame(win)
                  # 將頁面顯示出來
                  frm.pack()
                  
                  # 在窗口frm上布局
                  frm_1 = tkinter.Frame(frm)
                  # 在窗口的哪個地方
                  tkinter.Label(frm_1,text = '左上', bg = 'pink').pack(side = tkinter.TOP)
                  tkinter.Label(frm_1,text = '左下', bg = 'blue').pack(side = tkinter.TOP)
                  # 將窗口顯示出來
                  frm_1.pack(side = tkinter.LEFT)
                  
                  frm_2 = tkinter.Frame(frm)
                  tkinter.Label(frm_2,text = '右上', bg = 'red').pack(side = tkinter.TOP)
                  tkinter.Label(frm_2,text = '右下', bg = 'grey').pack(side = tkinter.TOP)
                  frm_2.pack(side = tkinter.RIGHT)
                  

                  15、表格

                  from  tkinter import ttk
                  
                  # 表格
                  tree = ttk.Treeview(win)
                  tree.pack()
                  
                  # 定義列,每列取個名字用來標識
                  tree['columns'] = ('姓名','年齡','身高','體重')
                  
                  # 設置列的寬度
                  tree.column('姓名',width=100)
                  tree.column('年齡',width=100)
                  tree.column('身高',width=100)
                  tree.column('體重',width=100)
                  
                  # 設置列的內容
                  tree.heading('姓名', text="name")
                  tree.heading('年齡', text="age")
                  tree.heading('身高', text="height")
                  tree.heading('體重', text="weigth")
                  
                  # 添加數據, 0代表第一行,如果再來一個0會將第一行的擠下去,text為行的內容,values為值
                  tree.insert('', 0, text = 'line1', values = ('tom', '28', '175', '75'))
                  tree.insert('', 0, text = 'line1', values = ('jim', '22', '165', '75'))
                  

                  16、目錄結構

                  from  tkinter import ttk
                  
                  # 表格
                  tree = ttk.Treeview(win)
                  tree.pack()
                  
                  # 最上層目錄
                  treeF1 = tree.insert('', 0, '中國', text='china', values=('F1'))
                  treeF2 = tree.insert('', 1, '美國', text='american', values=('F2'))
                  treeF3 = tree.insert('', 2, '英國', text='english', values=('F3'))
                  
                  # treeF1目錄下的子目錄
                  treeF1_1 = tree.insert(treeF1, 0, '黑龍江', text='黑龍江', values=('F1_1'))
                  treeF1_2 = tree.insert(treeF1, 1, '吉林', text='吉林', values=('F1_2'))
                  treeF1_3 = tree.insert(treeF1, 2, '武漢', text='武漢', values=('F1_3'))
                  

                  17、絕對布局

                  label1 = tkinter.Label(win,text = 'good' , bg = 'blue')
                  label2 = tkinter.Label(win,text = 'nice' , bg = 'red')
                  label3 = tkinter.Label(win,text = 'cool' , bg = 'yellow')
                  
                  label1.place(x = 10 , y = 10)
                  label2.place(x = 50 , y = 50)
                  label3.place(x = 100 , y = 100)
                  
                  

                  18、相對布局

                  label1 = tkinter.Label(win,text = 'good' , bg = 'blue')
                  label2 = tkinter.Label(win,text = 'nice' , bg = 'red')
                  label3 = tkinter.Label(win,text = 'cool' , bg = 'yellow')
                  
                  label1.pack(fill = tkinter.Y, side = tkinter.LEFT)
                  label2.pack(fill = tkinter.X, side = tkinter.TOP)
                  label3.pack(fill = tkinter.Y, side = tkinter.RIGHT)
                  

                  19、控件點擊事件

                  def func(event):
                      print(event.x, event.y)
                  # 鼠標點擊事件Button-1,2,3對應左、中、右,雙擊可以用Double-Button-1
                  buttonl = tkinter.Button(win, text='leftmouse button')
                  buttonl.bind('<Button-1>', func)
                  buttonl.pack()
                  

                  20、鼠標滑動事件

                  def func(event):
                      print(event.x,event.y)
                  # 鼠標滑動事件B1-Motion,1,2,3
                  buttonl = tkinter.Button(win, text='leftmouse button')
                  buttonl.pack()
                  
                  buttonl.bind('<B1-Motion>',func)
                  

                  21、鼠標進入離開事件

                  # <Leave>離開
                  Label = tkinter.Label(win,text="good", bg ='red')
                  Label.pack()
                  def func(event):
                      print(event.x,event.y)
                  Label.bind("<Enter>",func)
                  

                  22、響應所有按鍵

                  def func(event):
                      print(f'event.char = {event.char}')
                      print(f'event.keycode = {event.keycode}')
                  
                  Label = tkinter.Label(win,text="good", bg ='red')
                  # 設置焦點
                  Label.focus_set()
                  Label.pack()
                  
                  Label.bind("<Key>",func)
                  

                  二、控制窗口

                  1、下載模塊pip install pywin32

                  2、獲取窗口類和標題

                  3、每2秒顯示和隱藏窗口

                  import win32con, win32gui, time
                  
                  
                  while 1:
                  
                      DingDing = win32gui.FindWindow("StandardFrame_DingTalk", "釘釘")
                      win32gui.ShowWindow(DingDing, win32con.SW_SHOW)
                      time.sleep(2)
                      win32gui.ShowWindow(DingDing, win32con.SW_HIDE)
                      time.sleep(2)
                  

                  4、窗口隨機移動

                  import win32con, win32gui, time, random
                  
                  while 1:
                      DingDing = win32gui.FindWindow("StandardFrame_DingTalk", "釘釘")
                      x = random.randrange(600)
                      y = random.randrange(300)
                      #參數1:控制的窗體,參數2:大致方位,HWND_TOPMOST上方 ,參數3:位置x , 參數4:位置y,參數5:場地,參數6:寬度
                      win32gui.SetWindowPos(DingDing, win32con.HWND_TOPMOST, x, y, 30, 30, win32con.SWP_SHOWWINDOW)
                  

                  5、操作內存

                  隨時隨地學軟件編程-關注百度小程序和微信小程序
                  關于找一找教程網

                  本站文章僅代表作者觀點,不代表本站立場,所有文章非營利性免費分享。
                  本站提供了軟件編程、網站開發技術、服務器運維、人工智能等等IT技術文章,希望廣大程序員努力學習,讓我們用科技改變世界。
                  [python 窗口]http://www.yachtsalesaustralia.com/tech/detail-318723.html

                  贊(0)
                  關注微信小程序
                  程序員編程王-隨時隨地學編程

                  掃描二維碼或查找【程序員編程王】

                  可以隨時隨地學編程啦!

                  技術文章導航 更多>
                  国产在线拍揄自揄视频菠萝

                        1. <dd id="erndk"></dd>