在shell命令中使用字典条目作为变量



对于我的第一个真实项目,我使用Tkinter创建一个UI,当按下按钮时,会发生一些事情。

我已经创建了一个运行良好的UI——按钮是制作的,可以点击,但我希望每个按钮都有自己独特的功能——这就是我迷失的地方。

我有一个嵌套字典:

siteDict = {1: {'app1': 'explorer', 'app2': 'firefox', 'app3': 'google-chrome', 'app4': 'edge'},
2: {'site1': 'http://www.google.com', 'site2': 'http://www.yahoo.com', 'site3': 'http://altavista', 'site4': 'http://www.askjeeves.com', }}

我想将条目从字典传递到终端,而不是像现在这样手动定义每个条目:

def Expl_Goog():
os.system('echo Opening Explorer and navigating to Google (http://www.google.com). Please wait.')

那么,有没有一种方法可以将字典中的2个项目传递到终端命令中,而不是执行echo Opening Explorer and navigating to Google (http://www.google.com). Please wait.?{app1}和{site1}。

为大约12件事定义变量并不可怕,但项目的实际方面有相同类型的字典,但每个字典中有64个条目。

这是我到目前为止的代码,它是功能性的。按钮点击功能是通过";命令";按钮创建中的参数-ttk.Button(self.Explorer_labelFrame, text="google", command=Expl_Goog).grid(column=0, row=0, pady=2, sticky=tk.EW)

基于3.9.5

import tkinter as tk
from tkinter import ttk
import os
# Dictionary to house variables
siteDict = {1: {'app1': 'explorer', 'app2': 'firefox', 'app3': 'google-chrome', 'app4': 'edge'},
2: {'site1': 'http://www.google.com', 'site2': 'http://www.yahoo.com', 'site3': 'http://altavista', 'site4': 'http://www.askjeeves.com', }}
# Defining the action for the Tkinter button press to do
def Expl_Goog():
os.system('echo Opening Explorer and navigating to Google (http://www.google.com). Please wait.')
# Another definition - Explorer and Yahoo
def Expl_Yah():
os.system('echo Opening Explorer and navigating to Yahoo (http://www.yahoo.com). Please wait.')
def Fire_Goog():
os.system('echo Opening Firefox and navigating to Google (http://www.google.com). Please wait.')
def Fire_Yah():
os.system('echo Opening Firefox and navigating to Yahoo (http://www.yahoo.com). Please wait.')
# Build UI via Tkinter
class App(tk.Tk):
def __init__(self):
super(App, self).__init__()
self.title("Website Launcher")
self.minsize(200, 400)
#self.wm_iconbitmap('icon.ico')
self.lift()
self.attributes('-topmost', True)
self.update()
self.Explorer_labelFrame = ttk.LabelFrame(self, text="Explorer", labelanchor='n')
self.Explorer_labelFrame.grid(column=0, row=7, padx=20, pady=40)

self.Firefox_labelFrame = ttk.LabelFrame(self, text="Firefox", labelanchor='n')
self.Firefox_labelFrame.grid(column=2, row=7, padx=20, pady=40)

self.labels_Explorer()
self.labels_Firefox()
# Creating UI Buttons
# "command" argument makes clicking the button run the terminal command defined above
def labels_Explorer(self):
ttk.Button(self.Explorer_labelFrame, text="google", command=Expl_Goog).grid(column=0, row=0, pady=2, sticky=tk.EW)
ttk.Button(self.Explorer_labelFrame, text="yahoo", command=Expl_Yah).grid(column=0, row=1, pady=2, sticky=tk.EW)
ttk.Button(self.Explorer_labelFrame, text="altavista").grid(column=0, row=2, pady=2, sticky=tk.EW)
ttk.Button(self.Explorer_labelFrame, text="askjeeves").grid(column=0, row=3, pady=2, sticky=tk.EW)
def labels_Firefox(self):
ttk.Button(self.Firefox_labelFrame, text="google", command=Fire_Goog).grid(column=0, row=0, pady=2, sticky=tk.EW)
ttk.Button(self.Firefox_labelFrame, text="yahoo", command=Fire_Yah).grid(column=0, row=1, pady=2, sticky=tk.EW)
ttk.Button(self.Firefox_labelFrame, text="altavista").grid(column=0, row=2, pady=2, sticky=tk.EW)
ttk.Button(self.Firefox_labelFrame, text="askjeeves").grid(column=0, row=3, pady=2, sticky=tk.EW)
app = App()
app.mainloop()

如果我理解得对,你想在应用程序和网站之间做产品

from itertools import product
siteDict = {
1: {
"app1": "explorer",
"app2": "firefox",
"app3": "google-chrome",
"app4": "edge",
},
2: {
"site1": "http://www.google.com",
"site2": "http://www.yahoo.com",
"site3": "http://altavista",
"site4": "http://www.askjeeves.com",
},
}

for app, site in product(siteDict[1].values(), siteDict[2].values()):
s = "echo Opening {app} and navigating to {site}. Please wait.".format(
app=app, site=site
)
print(s)

打印:

echo Opening explorer and navigating to http://www.google.com. Please wait.
echo Opening explorer and navigating to http://www.yahoo.com. Please wait.
echo Opening explorer and navigating to http://altavista. Please wait.
echo Opening explorer and navigating to http://www.askjeeves.com. Please wait.
echo Opening firefox and navigating to http://www.google.com. Please wait.
echo Opening firefox and navigating to http://www.yahoo.com. Please wait.
echo Opening firefox and navigating to http://altavista. Please wait.
echo Opening firefox and navigating to http://www.askjeeves.com. Please wait.
echo Opening google-chrome and navigating to http://www.google.com. Please wait.
echo Opening google-chrome and navigating to http://www.yahoo.com. Please wait.
echo Opening google-chrome and navigating to http://altavista. Please wait.
echo Opening google-chrome and navigating to http://www.askjeeves.com. Please wait.
echo Opening edge and navigating to http://www.google.com. Please wait.
echo Opening edge and navigating to http://www.yahoo.com. Please wait.
echo Opening edge and navigating to http://altavista. Please wait.
echo Opening edge and navigating to http://www.askjeeves.com. Please wait.

编辑:只获取特定组合:

siteDict = {
1: {
"app1": "explorer",
"app2": "firefox",
"app3": "google-chrome",
"app4": "edge",
},
2: {
"site1": "http://www.google.com",
"site2": "http://www.yahoo.com",
"site3": "http://altavista",
"site4": "http://www.askjeeves.com",
},
}

def get(a, b):
s = "echo Opening {app} and navigating to {site}. Please wait."
return s.format(
app=siteDict[1]["app{}".format(a)], site=siteDict[2]["site{}".format(b)]
)

print(get(1, 3))

打印:

echo Opening explorer and navigating to http://altavista. Please wait.

相关内容

  • 没有找到相关文章

最新更新