當前位置:首頁 » 表格製作 » 怎樣添加菜單python
擴展閱讀
保存圖片也可以看接龍 2025-09-20 07:30:53
可以自由截屏的app 2025-09-20 07:09:03

怎樣添加菜單python

發布時間: 2022-12-22 18:25:12

⑴ python tkinter如何添加像word一樣的菜單欄

搜索 tkinter ribbon
比如
from tkinter import Widget
from os import path

class Ribbon(Widget):
def __init__(self, master, kw=None):
self.version = master.tk.call('package','require','tkribbon')
self.library = master.tk.eval('set ::tkribbon::library')
Widget.__init__(self, master, 'tkribbon::ribbon', kw=kw)

def load_resource(self, resource_file, resource_name='APPLICATION_RIBBON'):
"""Load the ribbon definition from resources.

Ribbon markup is compiled using the uicc compiler and the resource included
in a dll. Load from the provided file."""
self.tk.call(self._w, 'load_resources', resource_file)
self.tk.call(self._w, 'load_ui', resource_file, resource_name)

if __name__ == '__main__':
import sys
from tkinter import *
def main():
root = Tk()
r = Ribbon(root)
name = 'APPLICATION_RIBBON'
if len(sys.argv) > 1:
resource = sys.argv[1]
if len(sys.argv) > 2:
name = sys.argv[2]
else:
resource = path.join(r.library, 'libtkribbon1.0.dll')
r.load_resource(resource, name)
t = Text(root)
r.grid(sticky=(N,E,S,W))
t.grid(sticky=(N,E,S,W))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=1)
root.mainloop()
main()

⑵ 如何在Notepad++中配置python執行命令

我們經常會使用notepad++編寫python文件,但是編寫完以後怎麼在notepad++中直接執行呢?下面我給大家分享一下。

工具/材料

notepad++

首先我們打開notepad++文檔,在臨時文檔中編寫一句python內容,如下圖所示

然後我們按Ctrl+S對編輯的文檔進行保存,保存類型選擇Python即可,如下圖所示

接下來我們點擊頂部的運行菜單,然後點擊下拉菜單中的運行選項,如下圖所示

在彈出的運行設置界面中,我們在輸入框中輸入如下圖所示的命令,然後點擊保存按鈕

接下來我們給新建的python運行命令起一個名字,如下圖所示

配置好python執行命令以後,我們點擊運行菜單,這時你就會在下拉菜單中看到剛才配置的python運行命令,如下圖所示

接下來選擇我們配置的python運行命令,你就會看到彈出了CMD窗口,並且成功輸出了python文件中的內容,如下圖所示

最後為了使用方便,我們還可以打開快捷鍵設置界面,給python運行命令設置快捷鍵,如下圖所示

⑶ python圖形界面里添加不了讀者選項

圖形用戶界面(Graphical User Interface,簡稱 GUI,又稱圖形用戶介面)是指採用圖形方式顯示的計算機操作用戶界面。圖形用戶界面是一種人與計算機通信的界面顯示格式,允許用戶使用滑鼠等輸入設備操縱屏幕上的圖標或菜單選項,以選擇命令、調用文件、啟動程序或執行其它一些日常任務。與通過鍵盤輸入文本或字元命令來完成例行任務的字元界面相比,圖形用戶界面有許多優點。圖形用戶界面由窗口、下拉菜單、對話框及其相應的控制機制構成,在各種新式應用程序中都是標准化的,即相同的操作總是以同樣的方式來完成,在圖形用戶界面,用戶看到和操作的都是圖形對象,應用的是計算機圖形學的技術。
在設計GUI程序的過程中,需要對用戶界面進行渲染,達到色彩與便捷智能化一體。而在Python內置庫裡面,有一個自帶的就是tkinter庫,我們直接導入 使用即可。
簡單操作

import tkinter
top=tkinter.Tk()#生成一個主窗口
# 這裡面可以作為消息循環,添加窗口功能
label=tkinter.Label(top,text="圖形界面程序!")
label.pack()#將標簽label添加到窗口中
button1=tkinter.Button(top,text="按鈕1")
button1.pack(side=tkinter.LEFT)#將按鈕1添加到窗口裡
button2=tkinter.Button(top,text="按鈕2")
button2.pack(side=tkinter.RIGHT)#將按鈕2添加到窗口裡
top.mainloop()#進入消息循環
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10

tkinter組件介紹

import tkinter
import tkMessageBox

top = tkinter.Tk()

def helloCallBack():
tkMessageBox.showinfo("Hello Python", "Hello Runoob")

B = tkinter.Button(top, text="點我", command=helloCallBack)

B.pack()
top.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14

最完整的tkinter組件

向窗體中添加按鈕控制項

import tkinter
root=tkinter.Tk()#生成一個主窗口對象
button1=tkinter.Button(root,anchor=tkinter.E,#設置文本對齊方式
text="按鈕1",width=30,#設置按鈕寬度
height=7)
button1.pack()#將按鈕添加到主窗口
button2=tkinter.Button(root,text="按鈕2",bg="red")#設置背景按鈕色
button2.pack()
button3=tkinter.Button(root,text="按鈕3",width=12,height=1)
button3.pack()
button4=tkinter.Button(root,text="按鈕4",width=40,height=7,
state=tkinter.DISABLED)#設置按鈕為禁用
button4.pack()
root.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14

使用文本框控制項

在tkinter庫中可以實現信息接收和用戶的信息輸入工作,在Python程序中,使用tkinter.Entry和tkinter.text可以創建單行文本和多行文本框組件,通過傳遞一些屬性來解決顏色問題。

import tkinter
root=tkinter.Tk()
entry1=tkinter.Entry(root,
show="*"#設置顯示文本是星號
)
entry1.pack()
entry2=tkinter.Entry(root,show="$",width=50)
entry2.pack()
entry3=tkinter.Entry(root,bg="red",fg="blue")#設置文本框的前景色
entry3.pack()
entry4=tkinter.Entry(root,state=tkinter.DISABLED)
entry4.pack()
entry5=tkinter.Entry(root,selectbackground="red",selectforeground="gray")#分別設置文本背景色和文本前景色
entry5.pack()
edit1=tkinter.Text(root,selectbackground="red",selectforeground="gray")
edit1.pack()
root.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
使用菜單控制項

在使用菜單控制項的時候,和我們使用其他控制項有所不同,我們需要使用創建主窗口的方法config()將菜單添加到窗口中。

import tkinter
root=tkinter.Tk()
menu=tkinter.Menu(root)
# 添加主菜單選項
submenu=tkinter.Menu(menu,tearoff=0)
submenu.add_command(label="打開")
submenu.add_command(label="保存")
submenu.add_command(label="關閉")
menu.add_cascade(label="文件",menu=submenu)#設置標頭簽名稱

submenu=tkinter.Menu(menu,tearoff=0)
submenu.add_command(label="復制")
submenu.add_command(label="粘貼")
submenu.add_separator()
submenu.add_command(label="剪切")
menu.add_cascade(label="編輯",menu=submenu)

submenu=tkinter.Menu(menu,tearoff=0)
submenu.add_command(label="黑客模式")
submenu.add_command(label="植入病毒")
submenu.add_command(label="獲取密碼")
menu.add_cascade(label="幫助",menu=submenu)

root.config(menu=menu)#將菜單添加到主窗口
root.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
自己可定義不同的選項,之後我們在選項裡面嵌入不同的功能,這樣就達到了一個簡單圖形界面軟體的開發。

使用標簽控制項

import tkinter
root=tkinter.Tk()
label1=tkinter.Label(root,
# anchor=tkinter.E,#設置標簽文本位置
bg="yellow",#設置標簽的背景色
fg="blue",#設置標簽的前景色
text="我是王小王\n!",#設置標簽顯示的文本
justify=tkinter.CENTER,
width=40,#設置標簽寬度
height=5#設置標簽高度
)
label1.pack()#將標簽1添加到主窗口
label2=tkinter.Label(root,
text="你好\nPython!",#設置標簽顯示的文本
justify=tkinter.LEFT,
width=40,#設置標簽寬度
height=5#設置標簽高度
)
label2.pack()
label3=tkinter.Label(root,
text="你好\nPython!",#設置標簽顯示的文本
justify=tkinter.RIGHT,
width=40,#設置標簽寬度
height=5#設置標簽高度
)
label3.pack()
label4=tkinter.Label(root,
text="你好\nPython!",#設置標簽顯示的文本
justify=tkinter.CENTER,
width=40,#設置標簽寬度
height=5#設置標簽高度
)
label4.pack()

root.mainloop()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

使用單選按鈕和復選按鈕組件

import tkinter
root=tkinter.Tk()
r=tkinter.StringVar()#生成字元串變數
r.set("1")
radio=tkinter.Radiobutton(root,
variable=r,
value="1",#設置單選按鈕時的變數值
text="單選按鈕1",
)
radio.pack()
radio=tkinter.Radiobutton(root,
variable=r,
value="2",#設置單選按鈕時的變數值
text="單選按鈕2",
)
radio.pack()
radio=tkinter.Radiobutton(root,
variable=r,
value="3",#設置單選按鈕時的變數值
text="單選按鈕3",
)
radio.pack()
radio=tkinter.Radiobutton(root,
variable=r,
value="4",#設置單選按鈕時的變數值
text="單選按鈕4",
)
radio.pack()
c=tkinter.IntVar()#生成整型變數
c.set(1)
check=tkinter.Checkbutton(root,text="復選按鈕",
variable=c,#復選按鈕關聯的變數
onvalue=1,#設置復選按鈕時的變數值1
offvalue=2)#設置復選按鈕時的變數值2
check.pack()
root.mainloop()
print(r.get())
print(c.get())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

使用繪圖組件

import tkinter
root=tkinter.Tk()
canvas=tkinter.Canvas(root,
width=600,
height=480,
bg="white")#設置繪圖控制項的背景色
''''
...............
'''

1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
至於圖形界面的開發我們這里只是了解到了一個概念,比如如何自己設計,這樣渲染窗口,還有就是怎樣去把功能添加到圖形界面中,比如我們打開一個記事本,裡面有保存等功能我們可以輸入一系列的東西進行操作

⑷ 如何在win7下安裝Python及配置

1、首先,從網路搜索python官網下載適合自己電腦python版本。

⑸ python pyqt5 QTableWidget 添加右鍵菜單

'''
【簡介】
PyQT5的表格中支持右鍵菜單例子

'''

import sys
from PyQt5.QtWidgets import (QMenu, QPushButton, QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem,
QHeaderView)
from PyQt5.QtCore import QObject, Qt

class Table(QWidget):

if name == ' main ':
app = QApplication(sys.argv)
example = Table()
example.show()
sys.exit(app.exec_())

⑹ python tkinter 如何做一個如下所示的下拉菜單

Tkinter居然沒有這種組件,所以就只能模擬了

#! /usr/bin/python
# -*- coding: utf8 -*-
from Tkinter import *
class Select(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.label = Label(self, text="選擇項目")
self.listBox = Listbox(self, height=1)
self.button = Button(self, text='V', command=self.triggle)
self.hideList = True
for i in xrange(10):
self.listBox.insert(i, 'Item%d'%i)
self.label.grid(row=0, column=0, sticky=N)
self.listBox.grid(row=0, column=1, sticky=N)
self.button.grid(row=0, column=2, sticky=N)
self.grid()
def triggle(self):
self.hideList ^= 1
self.listBox.config(height=[self.listBox.size(), 1][self.hideList])
app = Select()
app.mainloop()

僅供參考。

⑺ 怎麼安裝python

python安裝教程具體如下:
1.確定電腦的系統類型。在安裝python之前,你要先確定一下你的電腦的系統類型及詳細配置,具體方法如下:點擊桌面上的快捷圖標「此電腦」,然後右擊「屬性」,在設備規格下仔細查看電腦的「系統類型」。
2.進入python官網(http://www.python.org)。你可以直接在瀏覽器里輸入python官網(http://www.python.org)的地址,也可以直接在網路或其他搜索引擎里輸入「python官網」,如下圖1-2所示。同樣可以快速進入python官網。如下圖1-3所示,由於此台電腦是64位的Windows10操作系統,所以點擊「Downloads」後選擇相應的python版本安裝即可。
3.選擇相應的python版本,下載python。根據此時這台電腦的系統類型(64位的Windows10操作系統),我們選擇相應的python版本,直接雙擊python解釋器文件進行下載即可。
4.此時,直接雙擊python解釋器文件,即可開始安裝python。

⑻ 怎麼用editplus配置python

首先下載安裝Editplus和python,安裝很簡單下載之後雙擊一直默認下一步就可以搞定,不過在安裝editplus的時候他們捆綁了兩個惡心的東西你可以取消。

打開editplus選擇工具-->配置用戶工具,點擊組名新建一個python組,緊接著點擊添加工具選擇-->應用程序
在菜單文字:python
命令:選擇你安裝python程序的路徑
參數:$(FileName)
初始目錄:$(FileDir)
最後點擊確定不過做完這里離成功還差點,在堅持一下曙光就在下面!

為了可以捕捉到錯誤信息和輸出結果強烈建議在第二步中的動作哪裡選擇捕捉輸出,再點擊旁邊的模板輸出,然後點擊取消-->使用默認輸出模式(U)在下面的選項中分別對應填寫下面的信息
正則表達式:File "(.+)", line ([0-9]+)
文件名:標記表達式1
行(L):標記表達式2
列(C):無
最後選擇確定

對不起是我忽悠了你其實還差一點,為了讓editplus配置的python的環境更牛叉的話,建議安裝一個python的插件。
下載之後解壓到你安裝editplus的安裝目錄,這個包裡面總過有三個文件我們要用到兩個,只要是為了讓editplus支持語法高亮,函數自動補全功能

在堅持一會就搞定了,程序員我們要的耐心不是嗎?這里很關鍵選擇菜單欄的-->工具-->配置用戶工具-->設置&語法
文件名擴展:py
語法文件:python_extd.stx(也就是editplus目錄下面的解壓的文件)
自動完成:python.acp

接下來點擊函數模板修改一下這里的正則表達式為:[ \t]*def[ \t].+:
確定之後在點擊Tab/縮進,然後填寫相應的東西這里主要是為了讓你的python代碼可以自動隨進,我們設置了默認是四個空格,這樣寫出來的代碼就很漂亮了!

最後一步你也可以設置一下書寫python的默認模板--選擇模板-->選擇python-->菜單欄輸入python-->文件路徑輸入editplus安裝目錄下面的template.py最後確定即可!

下面帶你去看一下代碼效果,選擇菜單欄的新建就可以看到有一個python選項了.在這里你簡單寫點代碼測試一下你的配置環境有沒有成功.例如我就故意把else下面的語句少寫了一個分號,就是為了看到報錯信息,在下面的錯誤上面雙擊就可以跳到錯誤行了很方便,不然上千行你去找累啊!寫完之後保存按ctrl+1就可以運行代碼了!
最後開始你的python的之旅吧祝你好運!

⑼ python文本菜單的程序

#!/usr/bin/envpython3#py3.6+
"""
#要求做一個系統菜單,輸入數字進入對應菜單,包含以下內容,正常操作不能報錯:
#菜單1:列印所有產品價格和庫存
#菜單2:修改產品價格
#菜單3:增加庫存
#菜單4:購買指定數量產品
#菜單5:增加新產品作為思考題
#菜單0:退出當前系統
"""

price={'vegetables':'3','eggs':'4','rice':'2'}#價格dict
stock={'vegetables':'0','eggs':'0','rice':'0'}#庫存dict

tip='''
1:列印所有產品價格和庫存
2:修改產品價格
3:增加庫存
4:購買指定數量產品
5:增加新產品作為思考題
0:退出當前系統
'''

defmain():
whileTrue:
globalprice,stock
a=input(f'Pleaseenteranumber:{tip} ').strip()
ifa=='0':
print('Exit!')
break
elifa=='1':
style='{:15}{:6}{:5}'
print(style.format('Name','price','stock'))
for(n,p),(_,s)inzip(price.items(),stock.items()):
print(style.format(n,p,s))
print()
elifa=='2':
whileTrue:
n=input(':')
ifninprice:
break
print('invalidinput!Shouldbe"{}".'.format(
'"or"'.join(price)))
p=input('enteranewpriceofthisproct:')
price[n]=p
elifa=='3':
whileTrue:
n=input(':')
ifninstock:
break
print('Invalidinput!Shouldbe"{}".'.format(
'"or"'.join(stock)))
whileTrue:
s=input(':')
try:
s=int(s)
break
except:
print('Invalidinput,mustbeainteger!')
stock[n]=str(int(stock[n])+s)
elifa=='4':
whileTrue:
n=input('enteraproctnametobuyit:')
ifninstock:
break
print('Invalidinput!Shouldbe"{}".'.format(
'"or"'.join(stock)))
whileTrue:
s=input('enteraintegerforhowmanytobuy:')
try:
s=int(s)
ifs<=0ors>int(stock[n]):
raise
break
except:
print('Invalidinput,mustbeapositiveintegerand'
'lessthan{}!'.format(stock[n]))
y=input('Youwanttobuy{}X{},whichcost{}?(y)/n'.format(
n,s,int(price[n])*s))
ify.strip().lower()in('y',''):
stock[n]=str(int(stock[n])-s)
print('Youpay{}andget{}{}'.format(int(price[n]*s),s,n))
elifa=='5':
print('Uncomplete... ')

if__name__=='__main__':
main()

⑽ 如何把一個Python腳本加入Windows右鍵菜單

先寫一個批處理:
myPath.bat
[plain] view plain
c:\Python34\python.exe d:\work\getPath.py %*
把這個批處理文件放到C:\Windows下面,便於調用。
再寫一個注冊表文件
getPath.reg
[plain] view plain
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\getPath\command]
@="myPath.bat \"%1\""
導入注冊表,右鍵就有getPath這個菜單了。