乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      用Python創(chuàng)建你第一個(gè)GIS程序[6]:python2.7 Tkinter 界面美化最終方案

       GIS薈 2021-09-19
      前言:有的人不喜歡 Tkinter 的一個(gè)重要原因就是其界面太丑,而我也偶爾覺得。在前后一年的時(shí)間中,針對(duì) Tkinter 的美化嘗試做了無數(shù)次,最終我選擇了以下這種方案...


      圖形化按鈕

      使用圖形化的按鈕比如這個(gè) 這個(gè)來替代原來的丑陋按鈕。

      # -*- coding:utf-8 -*-

      import Tkinter as tk
      import tkFileDialog
      from multiprocessing import Process
      import giscode


      class MyGUI(object):
         def __init__(self):
             self.root = tk.Tk()
             self.root.geometry("450x600+800+200")
             self.root.title("GIS") #設(shè)置程序名稱
             self.var = tk.StringVar()

             #<<注釋1>> 獲取圖像
             self.img1 = tk.PhotoImage(file="icon/shp2.gif")
             self.img2 = tk.PhotoImage(file="icon/ok2.gif")
             
             # run function
             self.create_widget()
             self.create_run_button()
             self.root.mainloop() # 執(zhí)行循環(huán)

         def create_widget(self):
             self.frame1 = tk.Frame(self.root, relief="raised", bd=3)
             self.frame1.pack(fill="x")
             self.entry = tk.Entry(self.frame1)
             self.entry.config(textvariable=self.var)
             self.entry.pack(
                 side="left",expand=True, fill="x", pady=8, padx=10
            )
             self.but = tk.Button(
                 self.frame1, text=u"輸入線要素", relief="flat", width=30
            ) #<<注釋2>> 設(shè)置按鈕的邊框樣式
             self.but.config(command=self.open_dialog)
             self.but.config(image=self.img1) #<<注釋3>>
             self.but.pack(side="right", pady=8)
         
         def open_dialog(self):
             varrr = tkFileDialog.askopenfilename()
             self.var.set(varrr)
             
         def create_run_button(self):
             # 生成下方的“運(yùn)行”按鈕
             self.bottom_frame = tk.Frame(self.root,relief="raised",bd=3)
             self.bottom_frame.pack(side="bottom",fill="x",anchor="s")
             self.ok_button = tk.Button(
                 self.bottom_frame,text=u"運(yùn)行", relief="flat", width=30
            ) #<<注釋4>>
             self.ok_button.pack(side="right", pady=8)
             self.ok_button.config(image=self.img2)#<<注釋5>>
             self.ok_button.config(command=self.run_multiprocessing)
         
         # def run(self):
         #     giscode.main(self.var.get())
         
         def run_multiprocessing(self):
             p = Process(target=giscode.main,
                         args=(self.var.get(),)
                        )
             p.start()
             print "PID:",p.pid
             
             
      if __name__ == '__main__':
         MyGUI()


      <<注釋1>>:
      獲取圖像。Tkinter 中的 PhotoImage 類用于獲取圖像。
      由于 Python2.7 中的 Tkinter 只支持 GIF 和PPM/PGM 格式,不支持 PNG 格式,所以最好不要選擇帶有陰影的圖標(biāo),GIF 是不支持陰影效果的。
      不然的話陰影就會(huì)變成這樣 和這樣 。
      原本外圈的陰影就會(huì)變成這種黑點(diǎn)點(diǎn)。

      如果想要 Tkinter 支持 PNG 格式的話有兩種方法,畢竟 PNG 格式的表現(xiàn)能力比 GIF 高不少。
      • 使用 PIL 包。缺點(diǎn)是引入的第三方包。

      • 升級(jí)到 Python3。但是 Python 無法使用 ArcPy。


      <<注釋2>> & <<注釋4>> :
      設(shè)置按鈕的邊框樣式。參數(shù) relief 控制邊框的樣式。一共有5種樣式,分別是 flat、raised、sunken、groove、ridge。


      現(xiàn)在流行扁平風(fēng)格嘛,就選擇了 flat 樣式 。
      <<注釋3>> & <<注釋5>> :
      將按鈕的圖像設(shè)置為選中的圖像。
      設(shè)置了圖像,先前設(shè)置的按鈕的文字就不會(huì)顯示了。

      舊界面

      新界面

      Note: 源代碼、icon圖標(biāo)和資料下載見最后。


      使用ThemedTk設(shè)置主題

      使用 ttkthemes 更改主題。
      ttkthemes 官方文檔:
      https://ttkthemes./en/latest/themes.html
      GitHub:
      https://github.com/TkinterEP/ttkthemes

      版本1

      # -*- coding:utf-8 -*-

      import Tkinter as tk
      import tkFileDialog
      from multiprocessing import Process
      from ttkthemes import ThemedTk #<<注釋1>>
      import ttk #<<注釋2>>
      import giscode


      class MyGUI(object):
         def __init__(self):
             # self.root = tk.Tk()
             self.root = ThemedTk(theme="arc") #<<注釋3>>
             self.root.geometry("450x600+800+200")
             self.root.title("GIS") #設(shè)置程序名稱
             self.var = tk.StringVar()

             self.img1 = tk.PhotoImage(file="icon/shp2.gif")
             self.img2 = tk.PhotoImage(file="icon/ok2.gif")
             
             # run function
             self.create_widget()
             self.create_run_button()
             self.root.mainloop() # 執(zhí)行循環(huán)

         def create_widget(self):
             self.frame1 = ttk.Frame(self.root)
             self.frame1.pack(fill="x")
             self.entry = ttk.Entry(self.frame1) #<<注釋4>>
             self.entry.config(textvariable=self.var)
             self.entry.pack(
                 side="left",expand=True, fill="x", pady=8, padx=10
            )
             # self.but = tk.Button(self.frame1, relief="flat")
             self.but = ttk.Button(self.frame1)
             self.but.config(command=self.open_dialog)
             self.but.config(image=self.img1)
             self.but.pack(side="right", pady=8, padx=6)
         
         def open_dialog(self):
             varrr = tkFileDialog.askopenfilename()
             self.var.set(varrr)
             
         def create_run_button(self):
             # 生成下方的“運(yùn)行”按鈕
             self.bottom_frame = ttk.Frame(self.root)
             self.bottom_frame.pack(side="bottom",fill="x",anchor="s")
             # self.ok_button = tk.Button(self.bottom_frame, relief="flat")
             self.ok_button = ttk.Button(self.bottom_frame)
             self.ok_button.pack(side="right", pady=8, padx=6)
             self.ok_button.config(image=self.img2)
             self.ok_button.config(command=self.run_multiprocessing)
         
         # def run(self):
         #     giscode.main(self.var.get())
         
         def run_multiprocessing(self):
             p = Process(target=giscode.main,
                         args=(self.var.get(),)
                        )
             p.start()
             print "PID:",p.pid
             
             
      if __name__ == '__main__':
         MyGUI()


      <<注釋1>>&<<注釋2>>:
      ttkthemes 模塊要和 ttk 模塊配合使用。
      因?yàn)?ttkthemes 只能修改使用 ttk 制作的組件。

      <<注釋3>>:
      支持 python2.7 的最新 ttkthemes 版本為2.4.0。經(jīng)測(cè)試,該版本支持 aquativo、arc、black、blue、clearlooks、elegance、equilux等主題。
      最喜歡的主題還是 arc 。


      <<注釋4>>:
      由于ttkthemes 只能修改使用 ttk 制作的組件的樣式,所以需要將 tk.Entry 替換為 ttk.Entry 即可。

      最終效果如下:


      版本2

      由于ttk.Button 修改按鈕邊框比較困難。
      你可以保留 tk.Button,relief 設(shè)置為 flat,其代碼和效果如下:

      # -*- coding:utf-8 -*-

      import Tkinter as tk
      import tkFileDialog
      from multiprocessing import Process
      from ttkthemes import ThemedTk #<<注釋1>>
      import ttk #<<注釋2>>
      import giscode


      class MyGUI(object):
         def __init__(self):
             # self.root = tk.Tk()
             self.root = ThemedTk(theme="arc") #<<注釋3>>
             self.root.geometry("450x600+800+200")
             self.root.title("GIS") #設(shè)置程序名稱
             self.var = tk.StringVar()

             self.img1 = tk.PhotoImage(file="icon/shp2.gif")
             self.img2 = tk.PhotoImage(file="icon/ok2.gif")
             
             # run function
             self.create_widget()
             self.create_run_button()
             self.root.mainloop() # 執(zhí)行循環(huán)

         def create_widget(self):
             self.frame1 = ttk.Frame(self.root)
             self.frame1.pack(fill="x")
             self.entry = ttk.Entry(self.frame1) #<<注釋4>>
             self.entry.config(textvariable=self.var)
             self.entry.pack(
                 side="left",expand=True, fill="x", pady=8, padx=10
            )
             self.but = tk.Button(self.frame1, relief="flat")
             # self.but = ttk.Button(self.frame1)
             self.but.config(command=self.open_dialog)
             self.but.config(image=self.img1)
             self.but.pack(side="right", pady=8, padx=6)
         
         def open_dialog(self):
             varrr = tkFileDialog.askopenfilename()
             self.var.set(varrr)
             
         def create_run_button(self):
             # 生成下方的“運(yùn)行”按鈕
             self.bottom_frame = ttk.Frame(self.root)
             self.bottom_frame.pack(side="bottom",fill="x",anchor="s")
             self.ok_button = tk.Button(self.bottom_frame, relief="flat")
             # self.ok_button = ttk.Button(self.bottom_frame)
             self.ok_button.pack(side="right", pady=8, padx=6)
             self.ok_button.config(image=self.img2)
             self.ok_button.config(command=self.run_multiprocessing)
         
         # def run(self):
         #     giscode.main(self.var.get())
         
         def run_multiprocessing(self):
             p = Process(target=giscode.main,
                         args=(self.var.get(),)
                        )
             p.start()
             print "PID:",p.pid
             
             
      if __name__ == '__main__':
         MyGUI()



      兩者在按鈕的邊框有一些不同之處。


      結(jié)束語

      以上,就是我一直在使用的美化方案,使用圖片來替代按鈕,然后使用 ttktkemes 模塊做進(jìn)一步的處理。
      美化之后的 Tkinter 界面是非常漂亮的

      《用Python創(chuàng)建你第一個(gè)GIS程序》該系列所有教程資料下載:

        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多