当我键入以下代码并运行程序以使用hostEntry2.get()
获取条目中的文本时,它将显示
NameError:名称
hostEntry2
未定义。
我试图删除for
循环(然后它可以工作),但是使用下面的代码将不起作用。
我希望应该显示在输入框中输入的文本。
from tkinter import *
host1 = ""
database1 = ""
user1 = ""
def db():
window1 = Tk()
window1.title("GIVE INFORAMATION ABOUT DATABASE")
F0 = Frame(window1, relief = SUNKEN)
F0.pack(side = LEFT)
l1 = ["host", "database", "user"]
l2 = ["HOST", "DATABASE", "USER"]
for i in range(len(l1)):
exec("%s2 = Label(F0, font = ('OpenSansCondensed', 12), text = \" ENTER THE %s:\", width = 30, anchor = \"w\")"%(l1[i], l2[i]))
exec("%s2.grid(row = %s, column = 0)"%(l1[i], i))
exec("%sEntry2 = Entry(F0, font = ('OpenSansCondensed', 12), textvariable = \"\", insertwidth = 4, justify = 'right')"%(l1[i]))
exec("%sEntry2.grid(row = %s, column = 1)"%(l1[i],i))
submitBtn = Button(F0, padx = 16, pady = 5, font = ('OpenSansCondensed', 10), width = 8, text = " OK ", command = lambda: [f for f in [submit_for_db(hostEntry2.get(), databaseEntry2.get(), userEntry2.get()), window1.destroy()]])
submitBtn.grid(row = 4, column = 0)
window1.mainloop()
def submit_for_db(host_enterred, database_enterred, user_enterred, password_enterred):
global host1
global database1
global user1
global password1
host1 = host_enterred
database1 = database_enterred
user1 = user_enterred
password1 = password_enterred
db()
答案 0 :(得分:0)
未定义函数#!/usr/bin/python3
# -*- coding: utf-8 -*-
import unittest
class TestDummy(unittest.TestCase):
"""A Dummy UnitTesting class."""
def test_failure(self):
"""Fails on test."""
self.fail(msg="Need a failure")
@unittest.skip("Need a Skipper")
def test_skipping(self):
"""Skippes on test."""
pass
def test_error(self):
"""Gives a error on test."""
self.not_a_thing()
def test_pass(self):
"""Need a test that passes."""
pass
def warp_test_suite(testcase_class):
"""Load tests from a specific set of TestCase classes."""
suite = unittest.TestSuite()
tests = unittest.defaultTestLoader.loadTestsFromTestCase(testcase_class)
suite.addTest(tests)
return suite
if __name__ == "__main__":
import json # For saving a JSON-file
# The test results dictionary, for the JSON.
result_value = {"Failures": 0, "Errors": 0, "Skipped": 0, "Test Runs": 0}
# Setup and run the Test
runner = unittest.TextTestRunner()
TextTestResult = runner.run(warp_test_suite(TestDummy))
# Passes the Result
result_value["Failures"] += len(TextTestResult.failures)
result_value["Errors"] += len(TextTestResult.errors)
result_value["Skipped"] += len(TextTestResult.skipped)
result_value["Test Runs"] += TextTestResult.testsRun
# Save the result to a JSON-file.
with open("result_data.json", 'w') as fp:
json.dump(result_value, fp, indent=3)
,我不推荐使用submit_for_db
函数
答案 1 :(得分:0)
这里似乎没有任何理由使用exec
。
l2 = ["HOST", "DATABASE", "USER"]
for i, label in enumerate(l2):
item = Label(F0, font=('OpenSansCondensed', 12), text=" ENTER THE %s:".format(label), width=30, anchor ="w")
item.grid(row=i, column=0)
entry = Entry(F0, font=('OpenSansCondensed', 12), textvariable="", insertwidth=4, justify='right')
entry.grid(row=i, column = 1)
entries.append(entry)
submitBtn = Button(F0, padx = 16, pady = 5, font = ('OpenSansCondensed', 10), width = 8, text = " OK ", command = lambda: [f for f in [submit_for_db([entry.get() for entry in entries]), window1.destroy()]])
submitBtn.grid(row = 4, column = 0)
(注意,不过我看不到您在哪里使用标签。)